admin管理员组文章数量:1332352
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
本文标签: javascriptReact reduxtoolkit not rerendering component when state is updatedStack Overflow
版权声明:本文标题:javascript - React @reduxtoolkit not re-rendering component when state is updated - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742322516a2453073.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论