-
Notifications
You must be signed in to change notification settings - Fork 61
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
Can't get updated state #12
Comments
It is because I'm not sure how we can resolve this in general. Basically the use of (edit) There was my misunderstanding at first. There is no issue with initialization. The use of |
I tried moving it to the I didn't understand what you meant with I tried to follow the fetch and onmount examples, but it didn't work |
Sorry. There might be something I misunderstand. https://codesandbox.io/s/github/dai-shi/react-hooks-global-state/tree/master/examples/05_onmount |
Yes: https://codesandbox.io/s/82kyyqw98j I just hard coded the setRates, because I won't integrate my API in codesandbox. But it replicates the issue.
I didn't try it, however I based my implementation using this one. |
@otaviobps Thanks for the code. It's nothing to do with initialization which I mentioned in the first comment. It's simply the usage of export const setRates = obj => {
setGlobalState('loading', false);
setGlobalState('rates', obj);
};
This is what I mean. const Routes = () => {
const setRates = useGlobalState('rates')[1];
const setLoading = useGlobalState('loading')[1];
useEffect(() => {
setRates(2);
setLoading(false);
}, []);
return (
<BrowserRouter>
<Switch>
<Route exact path="/" component={Main} />
<Route path="*" component={() => <h1>Page not found</h1>} />
</Switch>
</BrowserRouter>
);
}; But, this will trigger re-render if |
Closing this for inactivity. Feel free to reopen it. |
I followed some examples and tried to implement the global state. On any first page load, I want to call my back end and get some data. So in my App.js file I used
useEffect
to call a function to update the state:I debugged and I know that everything works right and the state is updated. However, on the page load, instead of accessing the updated state, I get my initial state.
Here is my Container that should receive the updated state, however receives the initial state:
What can be wrong?
Thanks!
The text was updated successfully, but these errors were encountered: