HTML script is loading AFTER react components

前端 未结 2 792
孤城傲影
孤城傲影 2021-01-06 01:50

My index.html



    

        
        

        
2条回答
  •  自闭症患者
    2021-01-06 02:03

    My suggestions:

    Change your google API script tag to be this, where you remove async and defer

    
    

    Get rid of your start function, which will now run the console.log fine and dandy, but the second bit of code will cause the same issue as it will also be running asynchronously.

    Modify your react code, so that the componentWillMount calls the contents of that function instead:

    componentWillMount() {
      gapi.load('auth2', () => {
        auth2 = gapi.auth2.init({
          client_id: 'urhaxorid.apps.googleusercontent.com',
          scope: 'profile email',
          onLoad: () => {
            this.setState({ mapLoaded: true });
          }
        });
      });
    }
    
    componentDidMount() {
      if (this.state.mapLoaded) {
        console.log(gapi.auth2.getAuthInstance());
      }
    }
    

    Please bear in mind that I don't know what the onLoad is for google apis, and I am not 100% sure how to best do the setState stuff, but it may be a starting block.

提交回复
热议问题