Context, Reducers and State Architecture
Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.
What you will be able to do
#- Share values through context without prop drilling.
- Provide context with the React 19 Context-as-provider syntax.
- Model complex state transitions with useReducer.
Overview
#Prop drilling and lifting
When two components far apart need the same data, the first instinct is to lift state to their nearest common ancestor. That works, but if the ancestor is high in the tree, every intermediate component re-renders and must forward props it does not use. This is prop drilling.
Context
Context lets a parent make a value available to any descendant without passing props through each level. In React 19 you can render the context object directly as the provider instead of writing <Context.Provider>.
const ThemeContext = createContext<"light" | "dark">("light");
function App() {
return (
<ThemeContext value="dark">
<Toolbar />
</ThemeContext>
);
}
function Toolbar() {
const theme = useContext(ThemeContext);
return <div data-theme={theme} />;
}
A context value change re-renders every consumer, so keep context values small and stable. If consumers only need a setter, split the setter into its own context so value changes do not re-render them.
useReducer
When state transitions are complex or depend on the previous state in many ways, a reducer centralizes the logic. The reducer is a pure function (state, action) => state, which makes transitions testable in isolation.
type State = { count: number };
type Action = { type: "inc" } | { type: "dec" } | { type: "reset" };
function reducer(state: State, action: Action): State {
switch (action.type) {
case "inc": return { count: state.count + 1 };
case "dec": return { count: state.count - 1 };
case "reset": return { count: 0 };
}
}
const [state, dispatch] = useReducer(reducer, { count: 0 });
Reducers must be pure: no mutation, no side effects. Return new state objects. Combine a reducer with context to give distant components a dispatch function without prop drilling.
Choosing the right tool
Local state for one component. Lifted state for a few siblings. Context for values needed across a subtree, especially stable ones like theme or a dispatch. An external store with useSyncExternalStore when data lives outside React and many components subscribe. Reach for a state library only when these are genuinely insufficient.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
Which components re-render when the theme value changes?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.tsxconst ThemeContext = createContext("light"); function App() { const [theme, setTheme] = useState("light"); return ( <ThemeContext value={theme}> <Header /> <Main /> </ThemeContext> ); } function Header() { return <h1>Title</h1>; } function Main() { const theme = useContext(ThemeContext); return <div data-theme={theme} />; }Exercise 2Implement
Implement a pure reducer for a counter supporting increment, decrement and reset, plus a component that dispatches increment.
starter.tsxtype State = { count: number }; type Action = { type: "inc" } | { type: "dec" } | { type: "reset" }; function reducer(state: State, action: Action): State { return state; }Reveal the reference answer
reference.tsxtype State = { count: number }; type Action = { type: "inc" } | { type: "dec" } | { type: "reset" }; function reducer(state: State, action: Action): State { switch (action.type) { case "inc": return { count: state.count + 1 }; case "dec": return { count: state.count - 1 }; case "reset": return { count: 0 }; } } function Counter() { const [state, dispatch] = useReducer(reducer, { count: 0 }); return <button onClick={() => dispatch({ type: "inc" })}>{state.count}</button>; }Authored expected output: Clicking the button increments the displayed count.
Each branch returns a new state object, keeping the reducer pure. The component dispatches a plain action object and reads the resulting state.
Exercise 3Explain
Explain why splitting a context into a value context and a dispatch context can reduce re-renders.
starter.tsxconst ValueContext = createContext(0); const DispatchContext = createContext<(a: Action) => void>(() => {});Reveal the reference answer
reference.tsxComponents that only dispatch do not need the value, and the dispatch function is stable across renders. If both live in one context, any value change creates a new context value and re-renders every consumer, including dispatch-only ones. Splitting keeps the dispatch context value stable, so dispatch-only consumers do not re-render when the value changes.Authored expected output: A written explanation of consumer re-render scope and value stability.
Context re-renders consumers when the value changes by Object.is. A stable dispatch function in its own context never changes, so its consumers stay put. This is a targeted optimization, not a default.
Exercise 4Interview
A teammate wants to put all app state in one global context. Argue for or against and propose an alternative.
starter.tsx// One AppContext holding user, theme, cart, notifications and filters.Reveal the reference answer
reference.tsxA single context couples unrelated features: any change re-renders every consumer and makes the value hard to type and test. Prefer local state where possible, lift only what is shared, and use separate contexts per concern with stable values. For genuinely global, frequently changing data, use an external store with useSyncExternalStore so components subscribe to the slices they need. Keep the single context only if the app is small and the value is stable.Authored expected output: A trade-off argument with a concrete alternative and its benefit.
Interviewers want a principled position: minimize shared state, split by concern, and choose the subscription model based on how often data changes and how many components read it.
Common misconceptions
#- Context is a state manager. Context is a transport for a value; state still lives in a component or store.
- Context avoids re-renders. Every consumer re-renders when the context value changes.
- Reducers may mutate state. Reducers must be pure and return new state.
- You must use Context.Provider in React 19. You can render the context object directly as the provider.
Follow-up practice
#- react-performance-and-identity
- react-data-fetching-and-transitions
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.