I have an issue with redux state being updated successfully, but react component not re-rendering, after some research I believe [forceUpdate()][1]
can solve my issue, but I am unsure of correct way to implement it i.e. after redux state updates. I tried looking up examples on github, but had no luck.
I have an issue with redux state being updated successfully, but react component not re-rendering, after some research I believe [forceUpdate()][1]
can solve my issue, but I am unsure of correct way to implement it i.e. after redux state updates. I tried looking up examples on github, but had no luck.
- 1 You almost never want a forceUpdate, almost always you just want a regular update. – Benjamin Gruenbaum Commented Feb 23, 2016 at 18:46
4 Answers
Reset to default 9As others have said, forceUpdate()
is basically a hack. You get updates for free whenever the props and state change. React and Redux work seamlessly together, so avoid any such hacks.
If your Redux state is updating correctly and your React view isn't, then it's most likely that you're not updating in an immutable fashion. Check your reducers.
It is possible that your issues is that the state you are updating is not a top level property. react calls a "shallowEqual" method to tell whether the props have updated. This means all top level properties, but if you are changing a deeper property and all the top level ones are the same then you will not rerender.
I'm not sure if this would be the case with redux maybe it's doing something more sophisticated but I know that is the workflow in react. It's worth taking a look.
That's not the solution you want.
Without looking at your code, it's impossible to tell what's actually wrong, but forceUpdate is not the solution. React will re-render if new data is placed in the state or props. If you're not re-rendering, it means you're data isn't getting all the way through.
Redux already doing forceUpdate
with connect decorator. Maybe you miss this point in your component. This behaviour is explained on the Usage with React section of official documentation.