This version of the board puts cards and dispatch in one context, as
one object: value={{ cards, dispatch }}. It works, but it has two problems:
JSX
// One context holding both the cards and dispatch.const BoardContext = createContext(null);export function BoardProvider({ children }) { const [cards, dispatch] = useReducer(boardReducer, starterCards); return <BoardContext value={{ cards, dispatch }}>{children}</BoardContext>;}export function useBoard() { return useContext(BoardContext);}
Header rendered appears again after every removal. Removing a card changes
cards, and cards is part of the context value. When a context's value
changes, every component that reads that context re-renders, whether or not
it uses the part that changed. Header only wants dispatch, but it reads a
value that contains cards, so it re-renders whenever cards does.
A second, separate problem hides in value={{ cards, dispatch }}: the braces
build a new object on every render of BoardProvider. Here the provider
only renders when cards changes, so it makes no extra difference. But if the
provider's parent re-rendered for some unrelated reason, the new object would
count as a changed value too, and every reader would re-render even with the
same cards. Wrapping the object in useMemo fixes that second problem only:
the value still changes with cards, so Header still re-renders on every
removal. To stop that, Header must read a context that doesn't contain
cards at all.
Fix it the way the last lesson did: two contexts, one for cards and one for
dispatch. dispatch never changes, so a component that reads only the
dispatch context never re-renders because of it. Replace BoardContext.jsx with:
JSX
import { createContext, useContext, useReducer } from "react";import { boardReducer } from "./boardReducer.js";import { starterCards } from "./data.js";const CardsContext = createContext(null);const DispatchContext = createContext(null);export function BoardProvider({ children }) { const [cards, dispatch] = useReducer(boardReducer, starterCards); return ( <CardsContext value={cards}> <DispatchContext value={dispatch}>{children}</DispatchContext> </CardsContext> );}export function useCards() { return useContext(CardsContext);}export function useBoardDispatch() { return useContext(DispatchContext);}
Then make Header use useBoardDispatch(), and Board use both hooks. Run,
remove two cards, and Header rendered appears only once, at the start.
For most screens, a few extra renders don't matter. Day 6 covers how to tell
when they do. Splitting state from dispatch costs nothing, though, so do it from
the start.
Pitfall 2: a missing provider fails silently, or strangely
In App.jsx, move <Header /> above <BoardProvider>, outside it. Run.
What you'll see
With the original one-object version, useBoard() returns the default,
null, and the app crashes with Cannot destructure property 'dispatch' … as
it is null. With the two-context version, useBoardDispatch() returns
null silently, and the crash only comes when you press Quick add:
dispatch is not a function. Either way, the message doesn't say what's
actually wrong.
Make the custom hooks check, and say what's wrong:
JavaScript
export function useBoardDispatch() { const dispatch = useContext(DispatchContext); if (dispatch === null) throw new Error("useBoardDispatch must be used inside <BoardProvider>."); return dispatch;}
Now the console names the mistake as soon as Header renders. This is why the
custom hooks exist: one place to check, with a message that says how to fix it.
Move <Header /> back inside.
Pitfall 3: context for everything
Context makes data flow harder to follow: a component can now depend on
something no prop mentions. Before reaching for it, try:
Passing props. Two or three levels is fine, and it's explicit.
Composition, the next lesson: passing components as children so the
middle layers don't need the data at all.
Exercise
Check the cards hook too
Give useCards() the same check, with the message
useCards must be used inside <BoardProvider>., and confirm it fires
when Board is rendered outside the provider.
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
A missing provider is a setup mistake. Detect it at the hook boundary so the error names the missing setup.
Hint 2: a first step
The same shape as useBoardDispatch: read the context, throw if it's null,
return it.
Show one solution
JavaScript
export function useCards() { const cards = useContext(CardsContext); if (cards === null) throw new Error("useCards must be used inside <BoardProvider>."); return cards;}
The default of null matters. Had the context been created with
createContext([]), a missing provider would give an empty board with no error
at all.
Completion is saved in this browser only. It does not measure mastery.