React Context Pattern
Share state across the component tree without prop drilling using React Context
When to Use
- Theme, locale, current user, or feature flags need to be accessible throughout the app
- Prop drilling through 3+ intermediate components that do not use the data
- State that changes infrequently (avoid for high-frequency updates without optimization)
- Building component libraries that need implicit configuration
Instructions
- Create a typed context with
createContext:interface AuthContextValue {
user: User | null;
signOut: () => void;
}
const AuthContext = createContext<AuthContextValue | null>(null);
- Create a Provider component that holds the state:
export function AuthProvider({ children }: { children: React.ReactNode }) {
const [user, setUser] = useState<User | null>(null);
return (
<AuthContext.Provider value={{ user, signOut: () => setUser(null) }}>
{children}
</AuthContext.Provider>
);
}
- Create a safe consumer hook that throws when used outside the provider:
export function useAuth(): AuthContextValue {
const ctx = useContext(AuthContext);
if (!ctx) throw new Error('useAuth must be used within AuthProvider');
return ctx;
}
- Place the provider at the appropriate level in the tree (app root, route boundary, or feature boundary).
Details
Context provides a mechanism for passing values through the component tree without prop drilling. It is not a replacement for state management — it is a mechanism for making existing state accessible.
Performance: All consumers of a context re-render when the context value changes. Split contexts by update frequency: { theme, toggleTheme } in one context, { user, signOut } in another.
Context vs prop drilling vs state management:
- Prop drilling: explicit, easy to trace, burdensome for deep trees
- Context: implicit, harder to trace, good for cross-cutting concerns
- State management (Zustand/Redux): explicit subscriptions, selectors, derived state
React 19: use(Context) can be called conditionally and inside if blocks, unlike useContext. Equivalent behavior, more flexibility.
Source
https://patterns.dev/react/context-pattern
Process
- Read the instructions and examples in this document.
- Apply the patterns to your implementation, adapting to your specific context.
- Verify your implementation against the details and edge cases listed above.
Harness Integration
- Type: knowledge — this skill is a reference document, not a procedural workflow.
- No tools or state — consumed as context by other skills and agents.
Success Criteria
- The patterns described in this document are applied correctly in the implementation.
- Edge cases and anti-patterns listed in this document are avoided.
1---2name: react-context-pattern3description: React Context Pattern4metadata:5 source: https://skillmd.com/skills/intense-visions/react-context-pattern6---7# React Context Pattern89> Share state across the component tree without prop drilling using React Context1011## When to Use1213- Theme, locale, current user, or feature flags need to be accessible throughout the app14- Prop drilling through 3+ intermediate components that do not use the data15- State that changes infrequently (avoid for high-frequency updates without optimization)16- Building component libraries that need implicit configuration1718## Instructions19201. Create a typed context with `createContext`:21 ```typescript22 interface AuthContextValue {23 user: User | null;24 signOut: () => void;25 }26 const AuthContext = createContext<AuthContextValue | null>(null);27 ```282. Create a Provider component that holds the state:29 ```typescript30 export function AuthProvider({ children }: { children: React.ReactNode }) {31 const [user, setUser] = useState<User | null>(null);32 return (33 <AuthContext.Provider value={{ user, signOut: () => setUser(null) }}>34 {children}35 </AuthContext.Provider>36 );37 }38 ```393. Create a safe consumer hook that throws when used outside the provider:40 ```typescript41 export function useAuth(): AuthContextValue {42 const ctx = useContext(AuthContext);43 if (!ctx) throw new Error('useAuth must be used within AuthProvider');44 return ctx;45 }46 ```474. Place the provider at the appropriate level in the tree (app root, route boundary, or feature boundary).4849## Details5051Context provides a mechanism for passing values through the component tree without prop drilling. It is not a replacement for state management — it is a mechanism for making existing state accessible.5253**Performance:** All consumers of a context re-render when the context value changes. Split contexts by update frequency: `{ theme, toggleTheme }` in one context, `{ user, signOut }` in another.5455**Context vs prop drilling vs state management:**5657- Prop drilling: explicit, easy to trace, burdensome for deep trees58- Context: implicit, harder to trace, good for cross-cutting concerns59- State management (Zustand/Redux): explicit subscriptions, selectors, derived state6061**React 19:** `use(Context)` can be called conditionally and inside `if` blocks, unlike `useContext`. Equivalent behavior, more flexibility.6263## Source6465https://patterns.dev/react/context-pattern6667## Process68691. Read the instructions and examples in this document.702. Apply the patterns to your implementation, adapting to your specific context.713. Verify your implementation against the details and edge cases listed above.7273## Harness Integration7475- **Type:** knowledge — this skill is a reference document, not a procedural workflow.76- **No tools or state** — consumed as context by other skills and agents.7778## Success Criteria7980- The patterns described in this document are applied correctly in the implementation.81- Edge cases and anti-patterns listed in this document are avoided.