Last active
May 9, 2022 08:18
-
-
Save crittermike/28fe4877ddabff65f589311fd5f8655c to your computer and use it in GitHub Desktop.
Using Google API (gapi) with React
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* global gapi */ | |
const API_KEY = 'YOURAPIKEYHERE'; | |
import React, { Component } from 'react'; | |
class App extends Component { | |
loadYoutubeApi() { | |
const script = document.createElement("script"); | |
script.src = "https://apis.google.com/js/client.js"; | |
script.onload = () => { | |
gapi.load('client', () => { | |
gapi.client.setApiKey(API_KEY); | |
gapi.client.load('youtube', 'v3', () => { | |
this.setState({ gapiReady: true }); | |
}); | |
}); | |
}; | |
document.body.appendChild(script); | |
} | |
componentDidMount() { | |
this.loadYoutubeApi(); | |
} | |
render() { | |
if (this.state.gapiReady) { | |
return ( | |
<h1>GAPI is loaded and ready to use.</h1> | |
); | |
}; | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
add this script tag in index.html
then type "gapi" in console of your tab in which your project is running to check if gapi is defined