Alex Rivera | Logout

React context re-renders entire tree - how to optimize with useMemo and selectors?

Asked 2026-04-11T15:20:00Z by Natalie Kim
29
I'm using React Context to share auth state across my app. Every time the token refreshes, every component that consumes the context re-renders even if they only care about the user object, not the token. I've tried splitting into two contexts but now I have provider hell. Is there a selector pattern like Redux?
Edit
Report

1 Answer

31
✔ Accepted
The selector pattern you want is use-context-selector or the newer React.use() with a memoized slice. But honestly, for auth state specifically, the cleanest approach is to split your context into AuthStateContext (user object, rarely changes) and AuthTokenContext (token, refreshes often). Components that only need the user subscribe to AuthStateContext and never re-render on token refresh. Provider nesting is fine - wrap them in a single AuthProvider component that encapsulates both.
answered 2026-04-11T17:50:00Z by Alex Rivera

Your Answer