Course contentsShow
← React
Lesson 1692 of 2,39641. State Management EcosystemFree lesson

Multiple Contexts for Performance

Split Context by update frequency to prevent unnecessary re-renders across unrelated consumers.

Multiple Contexts for Performance

You'll learn how splitting Context by update frequency prevents unnecessary re-renders across unrelated consumers.

The Re-Render Trap with Single Context

When you put all app-wide state into one Context, every consumer re-renders whenever any part of that Context changes—even if a component only cares about one slice of the data. Imagine a Context holding both theme (changes rarely) and notifications (updates frequently). Every theme consumer re-renders on every new notification!

The Solution: Split Context by Update Pattern

Create separate Contexts based on how often values change:

// Stable, rarely changes
const ThemeContext = createContext();

// Updates frequently
const NotificationsContext = createContext();

function App() {
  const [theme, setTheme] = useState('dark');
  const [notifications, setNotifications] = useState([]);

  return (
    <ThemeContext.Provider value={{ theme, setTheme }}>
      <NotificationsContext.Provider value={{ notifications, setNotifications }}>
        <Layout />
      </NotificationsContext.Provider>
    </ThemeContext.Provider>
  );
}

Now components consuming only ThemeContext won't re-render when notifications update, and vice versa.

The Pattern

Group related state by change frequency:

  • User preferences Context: Theme, language (stable)
  • Notifications Context: Alerts, messages (frequent)
  • Auth Context: User data (medium stability)

This isn't about number of consumers—it's about update patterns. If two pieces of state update together, they can share a Context. If they update independently, split them.

Key Takeaway: Split Contexts by update frequency, not just by domain—frequent updates in one Context shouldn't force re-renders in components that only need stable data from another Context.