Course contentsShow
← React
Lesson 862 of 2,39622. Context PerformanceFree lesson

When the Library is Worth It

Scenarios where use-context-selector provides measurable performance gains versus splitting context.

When the Library is Worth It

You'll learn: When use-context-selector delivers real performance wins versus the standard "split context" approach.

The Decision Point

You've seen two solutions to context performance problems: splitting contexts (built into React) and use-context-selector (external library). So when should you reach for the library?

The library shines when you have large, complex state objects with many consumers that each care about different pieces. Think of a dashboard application where one context holds user info, settings, notifications, and theme—and dozens of components scattered throughout the tree each read just one or two fields.

Splitting this into separate contexts works, but becomes unwieldy: you'd need UserContext, SettingsContext, NotificationsContext, ThemeContext... managing 4+ providers and contexts for what's logically one domain.

use-context-selector lets you keep one unified context while consumers selectively subscribe to only the slices they need. A component using useContextSelector(ctx, state => state.theme) won't re-render when state.notifications changes.

When Splitting Is Better

If your context naturally divides into 2-3 clear, independent concerns, just split the contexts. Zero dependencies, simpler mental model, works everywhere. The library adds bundle size (~2KB) and an external dependency.

The Sweet Spot

The library pays off when:

  • State is large and contains 5+ independent fields
  • You have 10+ consumers across the tree
  • Profiling shows context re-renders are a bottleneck
  • Splitting would create 4+ separate contexts that feel artificial

For smaller apps or contexts with 2-3 fields? Stick with splitting.

Key Takeaway: Use use-context-selector when a single, complex context has many consumers needing different slices—otherwise, splitting contexts is simpler and has no dependencies.