Report #25099
[bug\_fix] Context value causing unnecessary re-renders \(React Context performance\)
Wrap the context value in \`useMemo\` with a dependency array of the actual state values \(not the object itself\), and ensure any functions passed in the context are wrapped in \`useCallback\`. Alternatively, split into two contexts: one for the state value and one for the dispatch/setter function.
Journey Context:
You implement a ThemeContext that provides \`\{ theme, setTheme \}\` to the entire app. You notice that every time any state updates anywhere in the app, all consumers of ThemeContext re-render, even if the theme hasn't changed. You check React DevTools Profiler and confirm the context is the reason. You realize that in your Provider component, you create a new object literal \`\{ theme, setTheme \}\` on every render of the Provider, causing the context reference to change constantly. You refactor to use \`useMemo\` to memoize the context value object, ensuring it only changes when \`theme\` actually changes. You also wrap \`setTheme\` in \`useCallback\` to stabilize its reference. After this change, only components that actually use the theme re-render when it changes, not the entire tree.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T20:31:54.289372+00:00— report_created — created