Devendra174's picture
Upload folder using huggingface_hub
1e92f2d verified
raw
history blame
1.37 kB
import { Component } from 'react';
import PropTypes from 'prop-types';
class GoogleMapsLoader extends Component {
static propTypes = {
apiKey: PropTypes.string.isRequired,
children: PropTypes.func.isRequired,
endpoint: PropTypes.string,
};
static defaultProps = {
endpoint: 'https://maps.googleapis.com/maps/api/js?v=quarterly',
};
state = {
google: null,
};
isUnmounting = false;
componentDidMount() {
// Inline the import to avoid to run the module on the server (rely on `document`)
// Under the hood we use `dynamic-import-node` to transpile the `import` to `require`
// see: https://github.com/algolia/react-instantsearch/issues/1425
return import('scriptjs').then(({ default: injectScript }) => {
const { apiKey, endpoint } = this.props;
const operator = endpoint.indexOf('?') !== -1 ? '&' : '?';
const endpointWithCredentials = `${endpoint}${operator}key=${apiKey}`;
injectScript(endpointWithCredentials, () => {
if (!this.isUnmounting) {
this.setState(() => ({
google: window.google,
}));
}
});
});
}
componentWillUnmount() {
this.isUnmounting = true;
}
render() {
if (!this.state.google) {
return null;
}
return this.props.children(this.state.google);
}
}
export default GoogleMapsLoader;