4 ms·
Shallow equality is not a panacea -- it doesn't avoid updates when the content is the same, something which can easily happen. For example, say you have an act
by lobster_johnson 10y ago
Shallow equality is not a panacea -- it doesn't avoid updates when the content is the same, something which can easily happen.
For example, say you have an action that fetches a bunch of blog posts, then refetches later. If all the items are identical since the last fetch, you want to avoid rendering. So that means that whenever you deal with data updates, de-duplication has to occur at some point, preferably early on in the data layer when the surface area is minimal.
That de-duplication would need to "intern" data; i.e. if you fetch the new blog posts, check each of them if they are exactly the same as what's already cached (time stamps or version numbers could be used to save deep equality checks), and keep the existing object if that's the case.
Of course, you will still end up with lots of identical objects (different identity, but same content) purely because mutations generate them. For example, a user goes to a text field, types a character and then hits backspace. Now you've produced three different objects: version 0 (original data), version 1 (character added), version 2 (identical to v0, different identity). This will correctly re-render each version, of course. But let's say you had some other component (like a preview) that only updated every second. It would receive v0, v1, v2; at this point the delta of v2-v0 is zero, and there's no need to re-render; but a shallow equality check would cause it to. So de-duping needs to be built into all sorts of places you might not anticipate.