-
Notifications
You must be signed in to change notification settings - Fork 143
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
Error: Component must be wrapped with <Container.Provider> #79
Comments
Did you provide your app with a Provider? Sorry for the gioco di parole lol. Anyway, could you show us some code or even reproduce it to a codesandbox? e.g.: function App() {
return (
<Counter.Provider>
<CounterDisplay />
<Counter.Provider initialState={2}>
<div>
<div>
<CounterDisplay />
</div>
</div>
</Counter.Provider>
</Counter.Provider>
) |
Hi! Great wordplay and I learned a new expression! Yes, the app has a provider. To clarify, the container works great with hot loading until I change the code in the container. At that point i suspect it unmounts and remounts and loses context. I’m nesting 3 containers, it doesn’t happen for all of them, but it’s a container that I’m using in multiple nested components that’s causing problems. I’m away from my computer for the next few days, then I will try and make a code sandbox that demonstrates this - but I don’t think the sandbox will hot-load components with react-hot-loader! |
If it does not work, try with hook composition https://codesandbox.io/s/unstated-store-provider-k4h77?file=/src/store/index.js |
Oh, that looks interesting - I’ll try it and let you know. Thanks! |
I'm seeing this same issue with webpack hot module replacement (using Specifically, from my observations, it seems to happen when the state container definition is in the same module/file as the visual components that use that state. So with that theory, an obvious workaround is to make sure that my state container is defined in a file separate from everything else with minimal It may be possible for someone to come up with a fancier workaround using something like |
I came up with a minimal test case that shows this problem: https://github.com/elliottkember/unstated-hot-reload-test If you update the container file, you'll find that it crashes this way. To replicate this, clone that repository,
You should see that error message. It seems as though the container is being hot-reloaded but not all components that use it get the new component so the containers don't match. This is consistent for me under Parcel 1 and 2. I don't seem to have the same issue when I migrate that test bed repo to Webpack. The root cause may be something that Parcel is doing with hot module reloading. |
The one thing I've found that sort of alleviates this issue is this:
If I use a dynamic require to load my container in the component, when hot-reloading the container, the Provider and Component both get updated at the same time and will refer to the same Context object. I don't think this is intended. It seems as though React is replacing the component that requires this container, at the same time as it replaces the component that uses the Provider. This means that the component gets a I've tracked this all the way through my application, and if I I don't really know how to fix this, and this dynamic |
I've come up with a kooky little hack that works in some situations. I think it's related to context trees and loading order with hot-module-reloading. I've patched my own version of unstated, and I'm doing this (isDev is After the value is called from
Then I use that in
This has allowed me to hot-reload some of my containers. I will update this thread if this turns out to break everything, but it shouldn't affect anything in production, and has helped my workflow! |
I am also experiencing similar issues. : ( |
I'm using
react-hot-loader
, and whenever I update a container, I get the above error. It seems to happen when a sub-component uses the container that's provided by a parent component. I can't figure out how to fix this.It doesn't seem to matter whether I use MyContainer.useContainer() or useContainer(MyContainer) - still the same result.
The text was updated successfully, but these errors were encountered: