Skip to content
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

Setting component to rerender when script loaded #50

Open
krrishdholakia opened this issue Jan 22, 2020 · 2 comments
Open

Setting component to rerender when script loaded #50

krrishdholakia opened this issue Jan 22, 2020 · 2 comments

Comments

@krrishdholakia
Copy link

Currently I have a component: QuickAdd that needs the google autocomplete api to run

However, while the component renders, it doesn't seem like the autocomplete has finished loading. If i go to another component and then return to the QuickAdd component then it works, which makes me think it needs to rerender after the script is loaded.

The globalName is always passed as a prop and but is always undefined. What other listener can i use for this?

const callbackName = "onloadcallback"; const globalName = "quickAddLoader"; const url = https://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=places`;
const AsyncHoc = makeAsyncScriptLoader(url, {globalName: globalName})(QuickAdd);
export default AsyncHoc; `

@sephi-dev
Copy link

I have the exact same issue, would be glad on finding a solution

@sephi-dev
Copy link

sephi-dev commented Jan 23, 2020

Ok so I found a way to make it worked properly I guess. Here's what I did :

in the parent of my AsyncComponent

import Geosuggest from "@components/geosuggest";

const Component = () => {
  const [geosuggestActive, setActive] = useState(false);
  const GOOGLE_URL = `https://maps.googleapis.com/maps/api/js?key=${process.env.REACT_APP_GOOGLE_KEY}&libraries=places`;
  const AsyncGeosuggest = makeAsyncScriptLoader(GOOGLE_URL, { globalName: "google" })(
    Geosuggest,
  );

  return (
    <AsyncGeosuggest
      asyncScriptOnLoad={() => setActive(true)} // the important props for the callback
      isActive={geosuggestActive} // just pass the state, and display a placeholder instead nothing when not loaded
    />
  );
}

Hope It will help you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants