Codesandbox: =/src/App.js
Problem: When I update the state of my store the ponents are not re-rendered. In the above example you can see from the console that the data is correctly fetched, but the ponent is not re-rendered and keep rendering just Loading...
.
Expected behavior: When I update the state, the ponents are re-rendered.
I found a workaround by using useState in the ponent and subscribing to store changes like:
store.subscribe(() => setComponentState(store.getData())
This basically forces a re-render each time the data is updated.
I can mutate state: In the official docs they say that you can mutate the state since it has some magic that allows to do so (), hence that's not the issue.
Anyone has any idea about what's the appropriate way to make it work?
Codesandbox: https://codesandbox.io/s/condescending-wiles-funk0?file=/src/App.js
Problem: When I update the state of my store the ponents are not re-rendered. In the above example you can see from the console that the data is correctly fetched, but the ponent is not re-rendered and keep rendering just Loading...
.
Expected behavior: When I update the state, the ponents are re-rendered.
I found a workaround by using useState in the ponent and subscribing to store changes like:
store.subscribe(() => setComponentState(store.getData())
This basically forces a re-render each time the data is updated.
I can mutate state: In the official docs they say that you can mutate the state since it has some magic that allows to do so (https://redux-toolkit.js/usage/usage-guide#simplifying-reducers-with-createreducer), hence that's not the issue.
Anyone has any idea about what's the appropriate way to make it work?
Share Improve this question edited May 31, 2021 at 19:02 Linda Paiste 42.3k8 gold badges79 silver badges116 bronze badges asked Mar 26, 2021 at 0:07 devamatdevamat 2,5138 gold badges33 silver badges57 bronze badges2 Answers
Reset to default 3From the useStore docs, it states that the hook shouldn't be used in an app because it doesn't cause the ponent to re-render:
// EXAMPLE ONLY! Do not do this in a real app.
// The ponent will not automatically update if the store state changes
Instead, you should be using useSelector instead.
On that note, how you have your codesandbox example would cause an infinite re-render since dispatch(fetchData());
is called every time the redux state is updated. Instead, you'll want to place it within React.useEffect
.
For example, using useSelector
with useEffect
:
Got an answer on Github: https://github./reduxjs/redux-toolkit/issues/942#issuement-807813092
You should not be using the useStore hook. You should be using the useSelector hook, which subscribes to store updates:
- https://redux.js/tutorials/fundamentals/part-5-ui-react#reading-state-from-the-store-with-useselector
- https://redux.js/tutorials/essentials/part-2-app-structure#reading-data-with-useselector
- https://react-redux.js/api/hooks#useselector
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742322516a4422097.html
评论列表(0条)