Open LorinRenodeyn opened 2 months ago
I have to admit that I don't know how gatsby works and how it might relate to a newer version of yarn. To me this sounds like it has to have something to do with server-side-rendering or similar.
The main error-message ("<Map> can only be used inside an <ApiProvider> component."
), will only happen when the APIProvider
, or rather its context can't be found: https://github.com/visgl/react-google-maps/blob/b4afe30bc884c0f007fd9a811d864c061c279fae/src/components/map/index.tsx#L89-L96
Assuming that the components are in the correct hierarchy, there has to something in the way the application is bundled (maybe multiple instances of the react-google-maps library?) or executed (maybe the Context-provider isn't rendered in the client-side code or something like that?)
In order for us to help you debug this, there has to be something where we can see this issue happening. This should ideally be reduced to the core of the issue in a format that I can just checkout and run.
Thank you for taking the time to reply 🙂 I completely get your point of view. Unfortunately this is from a project I inherited that already went through several package updates before landing in my lap and I am not sure I could recreate the whole setup accurately to provide a proper reproduction repo. Nor do I have the time, sadly. That's why I tried to provide as many details as possible.
With the exact same codebase, component hierarchy, package versions and only different yarn versions the error popped up to my surprise. I'd guess the package resolution might be a factor? Perhaps a different (sub / peer) dependency being resolved somewhere on the newer yarn? This package was the only one throwing issues as far as I could tell.
Yeah, that's actually the only thing I can imagine right now where a different version of a package manager would have such an impact. I had this a couple of times in other projects and those issues are notoriously hard to debug.
When multiple instances of the @vis.gl/react-google-maps packages end up being bundled into your application, there could be a situation where the APIProvider
is created from one instance A (e.g. ./node_modules/@vis.gl/react-google-maps/dist/index.modern.js
) and the Map
component from another instance B (e.g. ./node_modules/some-component/node_modules/@vis.gl/react-google-maps/dist/index.modern.js
). The map component would then load APIProviderContext from the same package and can't find a context created from that APIProvider (since A.APIProvider !== B.APIProvider
). The same can also happen if your bundler produces multiple bundles and there are multiple instances of the very same package in different bundles.
You can check this with a bundle analyzer or see if there are multiple instances of the package in your node_modules (e.g. find ./node_modules -type d -path '*/@vis.gl/react-google-maps'
).
Description
I'm not 100% sure the issue is specific to this library or a react issue, but given the information I have at the moment it seems to point here.
I'm working on an NX monorepo project with a componentlibrary (and a few other libs) and 2 gatsby based websites. The gatsby project is running Webpack 5 and Gatsby 5.12. In the componentlibrary I created a component that makes use of the map, advancedmarker components and library hooks. In the component library's storybook and in the website project that consumes the library, I placed the in the root with the other providers. This with the aim to have the context available to all components (many levels deep)
The package manager for the project is Yarn (Classic; v1.22.22). Today I tried to upgrade the yarn version to the lasest berry (v4.3) release.
After changing the yarn version and reinstalling the packages. My component using the map threw an error: "
Reverting the yarn to classic version made the error disappear. Haven't gone through the whole site, but so far this seems to be the only component / usecase having issues.
Steps to Reproduce
Setup NX project with
yarn nodeLinker set to node-modules in yarnrc.yml
Terminal command:
yarn set version berry; rm -rf node_modules; yarn install; yarn clean; yarn develop
(yarn clean and yarn develop execute gatsby clean and gatsby develop respectively) Result: no errors in terminal, but opening the page with the component results in Unhandled Runtime Error message "Terminate the develop process in terminal and run
yarn set version classic; rm -rf node_modules; yarn install; yarn clean; yarn develop
Result: everything works as intended, no error messages.Nothing else changed apart from the yarn version and reinstalling packages between the 2 runs.
Environment
Logs