State Management · Middle · Question 6 of 75

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

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

Explanation

A new object literal is never equal to the previous one. Memoize: const value = useMemo(() => ({ user, login, logout }), [user, login, logout]) with useCallback for the functions (the React Compiler can do this automatically).

Further reading

Problem with a question

State Management · Middle · question 6

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