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

undefined store on "contextmenu" event #784

Closed
no-stack-dub-sack opened this issue Oct 21, 2022 · 2 comments
Closed

undefined store on "contextmenu" event #784

no-stack-dub-sack opened this issue Oct 21, 2022 · 2 comments

Comments

@no-stack-dub-sack
Copy link
Collaborator

no-stack-dub-sack commented Oct 21, 2022

This may be a bit specific to a certain use-case / library, but figured I'd report it anyway. If you check out this codesandbox and right click anywhere in the "Euros" column header or context menu, easy-peasy throws an error due to the store being undefined.

image

The error comes from the <Foo /> component which consumes store state via the useStoreState hook. I can easily work around this by just passing in the state I need, but found this interesting.

The context menu is rendered inside a portal, and I thought maybe this has something to do with it, but its odd that its only on the "contextmenu" event that this seems to happen. Otherwise, the component is working fine and is clearly subscribed to the store.

Any ideas?

Thanks!

@jmyrland
Copy link
Collaborator

Inspecting the code using React Dev Tools, it seems that the context is "lost" because the ContextMenu is rendered as a sibling of App:
image

The ContextMenu does not contain any StoreProvider, which in turn makes the useStoreState hook throw an error.

I was able to make it work by re-wrapping <Foo /> with a new provider, but with the same store:

                menuRenderer={() => {
                  return (
+                   <StoreProvider store={store}>
                      <Foo />
+                   </StoreProvider>
                  );
                }}

chrome_0HZafwmD6A

Hope that helps 👍

@no-stack-dub-sack
Copy link
Collaborator Author

Ah, so when its rendered via a left-click it's still within App's tree, but when you right-click it sits alongside App which explains why store seemed to only be undefined on right-click. This helps, thanks @jmyrland.

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