New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add another component to load the GMaps JS. #289
Conversation
tomchentw#275 lot of people confused about this
* Added Loading Libraries section tomchentw#275 lot of people confused about this * Links to Google Maps Javascript API, adds intro to library import, uses double quotes consistently (tomchentw#1) * Revert "Links to Google Maps Javascript API, adds intro to library import, us…" (tomchentw#2) * Revert "Revert "Links to Google Maps Javascript API, adds intro to library import, us…"" (tomchentw#3)
I like the HOC idea, but since the |
Maybe I misunderstand the question; the problem I wanted to solve was that the existing ScriptJsLoader requires the whole google maps component tree to be specified inside a single invokation of So the idea here is to provide some utility to have the google maps script tag be loaded for us while delaying the component construction until it is loaded. |
@miracle2k yes, and I imagine an HOC can help us avoid this issue. Sample code: function ScriptjsHOC(WrappedComponent) {
return class extends React.Component {
// some load logic
render() {
if (this.state.isLoaded) { return <WrappedComponent {...props } /> } else { return null; }
}
}
} and use it like: const MyMap = (props) => {
return (
<GoogleMapLoader………
);
}
export default ScriptjsHOC(MyMap) So the user will use the decorated component, and render |
You mean that when used as a decorator, the child-as-function thing is not necessary? Yes, you're right. The child-as-function is only necessary if you want to have the and the components created in the same parent's render(). If you don't intend to support that, we can avoid this part, and maybe only go with a decorator. |
@miracle2k I believe decorators/HOCs are the right way to go. |
@miracle2k Are you willing to update your PR to use decorators? :) |
We'll provide a HOC approach in 6.0.0. Follows up #318 ! |
This is my approach to solve #281 - letting the library load the Google Maps JavaScript file for me when I need it but still being able to access the
google
namespace. This is inspired by howreact-motion
uses a single-function child.It's also nice to use this with the decorator, from my project: