I am trying to wrap a react component that requires the google maps api, with a makeAsyncScriptLoader
from react-async-script
.
In the same file (if that makes a difference):
class SearchBar extends Component {
/* Some stuff requiring the maps api */
}
const callbackName = "onloadcallback";
const URL = `https://maps.googleapis.com/maps/api/js?key=AIzaSyDSn_vNbNZzrcFxl8bV3MH1j0kuoLVsOCQ&callback=${callbackName}`;
const globalName = "foo";
export default makeAsyncScriptLoader(URL, {
callbackName: callbackName,
globalName: globalName
})(SearchBar);
My understanding, is that I should then be able to call the component from another file by importing SearchBarWrapper
from the above file, and using it as <SearchBarWrapper />
.
Basically, it doesn't work. The component tries to load without the script, and fails.
According to documentation the following properties could be specified to ensure JavaScript resource is finished loading:
or
For the case of
asyncScriptOnLoad
event the following HOC could be introduced to ensure a wrapped component is rendered once JavaScript resource is loaded:Now Google Maps component could be rendered like this:
Demo