State Management · Senior · Question 13 of 75

What does a memoized selector from createSelector need to be effective with arguments?

const selectByCategory = createSelector(
  [(s) => s.products, (s, category) => category],
  (products, category) => products.filter((p) => p.category === category),
);

Explanation

The default cache stores the latest inputs, so alternating arguments in several components ('books', 'toys') recomputes every time. Create a selector per component (useMemo(makeSelector, [])) or configure a larger cache (weakMapMemoize is the default in Reselect 5).

Further reading

Problem with a question

State Management · Senior · question 13

Test yourself

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

Take this quiz