You will learn
Reading from a normalized store builds a fresh object tree every time. Memoised components compare by identity, so they all re-render even though most of their data is unchanged.
A selector here is called as selector(state, previous). The second argument is its own previous result, so it can hand back the parts that did not move instead of returning all-new objects.
Try it
Notice that only Ada’s counter moves on the right. Grace and Alan came back as the objects they already were, so their memo held.
An equality function cannot do this. It can only answer whether to keep the whole result; recycling needs the previous value itself.
(state) => project(state)freshEveryone re-renders. Ada’s count moved, so the projection is new, and so is every object inside it.
(state, previous) => recycleNodesInto(previous, …)recycledOnly Ada re-renders. Grace and Alan came back as the same objects, so their memo held.