Skip to main content
Context provides dependency injection for components. Create a context, wrap a subtree with a Provider, and read it with useContext(). No prop threading required.

createContext()

Create a context object that can hold a typed value.

Signature

Parameters

Returns

Context<T> — an object with a Provider method for providing values and internal state for useContext() lookups.

useContext()

Read the current value from the nearest Provider in the component tree.

Signature

Parameters

Returns

The current context value from the nearest Provider, the default value if no Provider is active, or undefined if neither exists.

Provider

Provide a value to all descendants. Two usage patterns are supported:

JSX usage

Callback usage


Pattern: typed accessor hook

Always create a use* wrapper that throws on missing Provider. This gives consumers a clear error instead of silent undefined.

Types