I was having an issue where selecting certain values out of my store would trigger a rerender with any store value update, even if I was selecting values that weren't updating. It turns out, the issue was that I was doing this:
const transformedValues = useSelector(state => state.some.array).map(value => value);
As the answer to the above referenced question points out, this breaks referential equality of transformedValues
, causing the component to update.
But when I do this, there is no issue, and no unecessary rerender:
const valuesFromStore = useSelector(state => state.some.array);
const transformedValues = valuesFromStore.map(value => value);
Why? What is the difference in these two tactics that in the first one, referential equality of the selector is broken (causing rerender), and in the second, it does not cause a rerender? What is going on "under the hood" of redux and javascript to make a difference here?