javascript - React @reduxtoolkit not re-rendering component when state is updated - Stack Overflow

Codesandbox: =srcApp.jsProblem: When I update the state of my store the ponents are not re-rendered.

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 badges
Add a ment  | 

2 Answers 2

Reset to default 3

From 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信