3

Google API JS library returns a callback on successful load.

<script src="https://apis.google.com/js/client.js?onload=callback"></script>

Can someone explain how to integrate this callback into React application with server-side rendering? In vanilla js it will something like this:

function callback() {
  gapi.auth.authorize({...});
}

1 Answer 1

3

It depends on exactly what you're doing. Let's assume you want to enact a state change on that callback. You could put the following in your React component:

componentWillMount() {
    if (typeof window === 'undefined') {
        return; // client side only
    }
    window.callback = () => {
        this.setState({
            isAuthorized: true
        });
    };
}

Just make sure this callback is defined before you call the remote resource. i.e. Put your React scripts before the Google script.

Sign up to request clarification or add additional context in comments.

6 Comments

Hmm... even if render function returns false I should include component declaration into the html code, correct? Is it possible to achieve the same thing without doing this?
I'm afraid I don't understand what you're asking here. Perhaps you might like to provide some detail on what you're trying to achieve.
I wrote a component, here is my code: jsfiddle.net/t9d2d2gp Now I can use it like this: import api from 'comp'. Question is, how to use it without adding html tags <api></api>
You don't need to create a brand new React component. Simply define the above componentWillMount within one of your existing React components.
Well it is an API, so I thought about defining API service, so I can call it from different parts of my code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.