Reuse Hooks with Shared State using Context

2026-02-09

I recently encountered a problem where I wanted to reuse a hook's logic but ensure that all consumers shared the exact same state. A simple hook call in multiple components creates isolated state for each instance. To solve this, I designed a solution using a ContextProvider architecture.

The Architecture

The solution revolves around a ContextProvider component that performs two critical functions:

  1. Stores the source of truth locally. This is the raw state that drives the application.
  2. Provides memoized methods. These methods calculate the final, presentation-ready state from the raw source of truth.

This provider then:

Why This Works

This approach has several key benefits:

This pattern is cleaner than Redux for simple shared state and more robust than simple prop drilling or global variables.