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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
hooks: useContext with useState not updating #14708
Comments
It looks to me like your Provider's value only influences context for components inside of it. By the way, if you didn't try to cheat the Hooks rules with code like |
hey Dan! thanks for the quick response (: just fixed it for me. btw: solution: I just solved it by modifying that quirky |
Aaah you're right I misread your example. Yeah |
How to fix it. I think I'm in a similar situation.
Alert is working fine after settimeout but re-rendering isn't happening. It only re-renders when I'm rendering state variable inside context i.e.
But I cannot do that, I need this state data after API call to be availble via context for other components. |
If I have to get data from API then it has to be called before |
I was assuming, I'm wrapping it correctly, but suddenly I realized and checked and found that I wrapped it in wrong container.. My bad.. I'm good here... thanks.. |
Do you want to request a feature or report a bug?
seems it's a bug. 馃槙
What is the current behavior?
Nested context provider and
useContext
hooks seems to be conflicting, updates get discarded.What is the expected behavior?
When connecting to a context, it should update whenever it's
value
changes.Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
18.8.0-alpha.1
(also reproduced on16.7.0-alpha.0
)chrome 71
more details
While working on a cleanup of a localStorage "connection",
I tried to mix 2 articles (
[1]
&[2]
) from the official react documentation, I've implemented it with hooks, but the value seems not to be passing through.I've put up a streamlined demo on codesandbox
[3]
.The actual implementation is only a couple of lines more (parsing it from and stringifying it to JSON).
Workarounds that I found:
setValue
function, it actually works.[1]
about avoiding creating new values.componentDidUpdate
instead ofuseEffect
.Is there anything that shouldn't work on the code below? the effect gets triggered with the changes,
but the value doesn't get updated on the components that consume via hook. see repro code
[3]
[1]
: https://reactjs.org/docs/hooks-faq.html#how-to-avoid-passing-callbacks-down[2]
: https://reactjs.org/docs/context.html#updating-context-from-a-nested-component[3]
: https://codesandbox.io/s/0yzjr8vnrvThe text was updated successfully, but these errors were encountered: