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)
);

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.

Further reading

Problem with a question

React · Middle · question 40

Test yourself

Take the React · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz