4 ms·
A hard part of React is determining when and what to re-render. It's one of the main things the state-management tools solve (Redux, MobX, etc). For example, i
by always_good 9y ago
A hard part of React is determining when and what to re-render. It's one of the main things the state-management tools solve (Redux, MobX, etc).
For example, imagine the naive example where you just have a global `const store = { ... }` and all components read from it and update it. You could tell everything to re-render on any change, but that's slow. So you can then implement `shouldComponentUpdate` for each component to manually check if their dependent data changed. But that may be a lot of work where each component needs to know quite a bit about the data.
For instance, how do you check if two arrays are the same and without walking both arrays? Depending on your data, it may be as simple as comparing `item.id` of the first item in each array. But it might not be.
On the other hand, you can tell if two immutable datastructures are equal with reference equality (obj1 === obj2). If all of your components take immutable data, then you can abstract away `shouldComponentUpdate` with a reference comparison. This is what React's PureComponent does.
That's the main use-case. Not as many obvious use-cases on the server side in Javascript.