Course contentsShow
← React
Lesson 2254 of 2,39649. Performance: Identifying ProblemsFree lesson

Splitting Context by Domain Concern

Organizing multiple focused contexts (theme, auth, config) instead of one monolithic context to isolate updates.

Splitting Context by Domain Concern

What you'll learn: How to organize multiple focused contexts by business domain (theme, auth, config) instead of cramming everything into one giant context, preventing unnecessary re-renders across unrelated features.

The Problem with Kitchen-Sink Context

You've already learned that splitting context by update frequency (fast-changing vs slow-changing data) prevents cascading re-renders. But there's another critical dimension: domain concern.

Imagine a single AppContext that holds theme settings, user authentication, feature flags, and UI state. Even though these might all update infrequently, they're unrelated concerns. When your theme changes, components that only care about authentication still re-render because they're consuming the same context.

Organize by Business Purpose

Split contexts by what they represent:

// ❌ One monolithic context
const AppContext = createContext({ 
  theme, user, config, notifications 
});

// ✅ Separate domain contexts
const ThemeContext = createContext(theme);
const AuthContext = createContext(user);
const ConfigContext = createContext(config);
const NotificationContext = createContext(notifications);

Now components subscribe only to the data they need. A <Button> using ThemeContext won't re-render when the user logs out. A <ProfileMenu> using AuthContext won't re-render when theme changes.

Real-World Analogy

Think of broadcast radio stations. Instead of one station broadcasting news, music, weather, and sports all mixed together (forcing everyone to listen to everything), you have separate stations. Listeners tune into only the stations they care about.

Isolation of Updates

When you update theme settings, only theme consumers react. When auth state changes, only auth-aware components update. This domain isolation prevents the "everything re-renders" problem even when all contexts live at the app root.

Key Takeaway: Splitting context by domain concern (theme, auth, config) creates natural update boundaries where components only re-render when their specific domain changes, not when unrelated data updates.