React · Middle · Question 40 of 75
Why does this selector cause a re-render on any action in the store?
const done = useSelector(state =>
state.todos.filter(t => t.done)
);- ✓Correct answer:
filterreturns a new array every time - 2
useSelectoralways re-renders on any action - 3
filteris too slow to call inside a selector
Explanation
useSelector runs after every dispatch and compares the result by reference. A new array never equals the previous one. The fix is a memoized createSelector selector, which returns the same array as long as state.todos hasn't changed.