Skip to content

Instantly share code, notes, and snippets.

@liondancer
Forked from crittermike/App.js
Created August 15, 2017 09:45
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save liondancer/d0d519dd27b0f44cc6b02068d3b3742a to your computer and use it in GitHub Desktop.
Save liondancer/d0d519dd27b0f44cc6b02068d3b3742a to your computer and use it in GitHub Desktop.
Using Google API (gapi) with React
/* 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;
@surajsharma
Copy link

elegant!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment