State Management · Middle · Question 58 of 75

How do you make a component re-render only when one post from a cached list changes?

const { post } = useGetPostsQuery(undefined, {
  selectFromResult: ({ data }) => ({ post: data?.find((p) => p.id === id) }),
});

Explanation

The hook compares the selected value shallowly and skips re-renders when it is unchanged. Return stable references from the selector.

Further reading

Problem with a question

State Management · Middle · question 58

Test yourself

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

Take this quiz