React · Middle · Question 21 of 75

Why do all context consumers re-render on every provider render?

<AuthContext value={{ user, login, logout }}>
  {children}
</AuthContext>

Explanation

React compares value via Object.is. An object literal is always new, so every provider render wakes up all consumers. The fix is useMemo for the value (and useCallback for functions), or splitting the context into parts.

Further reading

Problem with a question

React · Middle · question 21

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