Skip to content

Checkpoint: Board Preferences

The Course
•
20 min study

Close the earlier lessons and build this from the brief. The starter passes showMinutes from App through Board and Column to CardItem.

The brief

Replace the drilling with a preferences context:

  • A Preferences.jsx file with a PreferencesProvider that holds { showMinutes: true, compact: false } in a reducer, with the actions minutes_toggled and compact_toggled.
  • State and dispatch in separate contexts, read through two custom hooks, usePreferences() and usePreferencesDispatch(). Each throws a clear error when used outside the provider.
  • A Toolbar component with two checkboxes, Show minutes and Compact. Compact cards have less padding.
  • Board and Column no longer mention any preference.

Acceptance checks:

  • Unticking Show minutes hides every card's minutes.
  • Rendering Toolbar outside the provider shows your error message in the console.
  • No preference props remain in Board.jsx or Column.jsx.
Your workspace React · 7 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { useState } from "react"; import Board from "./Board.jsx"; import { starterCards } from "./data.js"; export default function App() { const [showMinutes, setShowMinutes] = useState(true); return ( <main> <h1>Study Board</h1> <div className="toolbar"> <label> <input type="checkbox" checked={showMinutes} onChange={(e) => setShowMinutes(e.target.checked)} /> Show minutes </label> </div> <Board cards={starterCards} showMinutes={showMinutes} /> </main> ); } import Column from "./Column.jsx"; import { columns } from "./data.js"; export default function Board({ cards, showMinutes }) { return ( <div className="board"> {columns.map((column) => ( <Column key={column.id} title={column.title} showMinutes={showMinutes} cards={cards.filter((card) => card.column === column.id)} /> ))} </div> ); } export default function CardItem({ card, showMinutes }) { return ( <li className="card"> <strong>{card.title}</strong> {showMinutes && <div className="muted">{card.minutes} min</div>} </li> ); } import CardItem from "./CardItem.jsx"; export default function Column({ title, cards, showMinutes }) { return ( <section className="column"> <h2> {title} <span className="count">({cards.length})</span> </h2> <ul className="cards"> {cards.map((card) => ( <CardItem key={card.id} card={card} showMinutes={showMinutes} /> ))} </ul> </section> ); } export const columns = [ { id: "todo", title: "To do" }, { id: "doing", title: "Doing" }, { id: "done", title: "Done" }, ]; export const starterCards = [ { id: 1, title: "Read about reducers", column: "todo", minutes: 20 }, { id: 2, title: "Draw the component tree", column: "doing", minutes: 15 }, { id: 3, title: "Practice useState", column: "done", minutes: 30 }, { id: 4, title: "Try context", column: "todo", minutes: 25 }, ]; body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the shape

It's the board-context pattern with different state: createContext(null) twice, a provider with useReducer, and two hooks that check for null.

Hint 2: compact styling

An inline style is enough: style={compact ? { padding: "0.15rem 0.5rem" } : undefined} on the card's <li>.

Show one solution
One solution React · 9 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import Board from "./Board.jsx"; import Toolbar from "./Toolbar.jsx"; import { PreferencesProvider } from "./Preferences.jsx"; import { starterCards } from "./data.js"; export default function App() { return ( <PreferencesProvider> <main> <h1>Study Board</h1> <Toolbar /> <Board cards={starterCards} /> </main> </PreferencesProvider> ); } import Column from "./Column.jsx"; import { columns } from "./data.js"; export default function Board({ cards }) { return ( <div className="board"> {columns.map((column) => ( <Column key={column.id} title={column.title} cards={cards.filter((card) => card.column === column.id)} /> ))} </div> ); } import { usePreferences } from "./Preferences.jsx"; export default function CardItem({ card }) { const { showMinutes, compact } = usePreferences(); return ( <li className="card" style={compact ? { padding: "0.15rem 0.5rem" } : undefined}> <strong>{card.title}</strong> {showMinutes && <div className="muted">{card.minutes} min</div>} </li> ); } import CardItem from "./CardItem.jsx"; export default function Column({ title, cards }) { return ( <section className="column"> <h2> {title} <span className="count">({cards.length})</span> </h2> <ul className="cards"> {cards.map((card) => ( <CardItem key={card.id} card={card} /> ))} </ul> </section> ); } export const columns = [ { id: "todo", title: "To do" }, { id: "doing", title: "Doing" }, { id: "done", title: "Done" }, ]; export const starterCards = [ { id: 1, title: "Read about reducers", column: "todo", minutes: 20 }, { id: 2, title: "Draw the component tree", column: "doing", minutes: 15 }, { id: 3, title: "Practice useState", column: "done", minutes: 30 }, { id: 4, title: "Try context", column: "todo", minutes: 25 }, ]; import { createContext, useContext, useReducer } from "react"; const initialPreferences = { showMinutes: true, compact: false }; function preferencesReducer(prefs, action) { switch (action.type) { case "minutes_toggled": return { ...prefs, showMinutes: !prefs.showMinutes }; case "compact_toggled": return { ...prefs, compact: !prefs.compact }; default: throw new Error(`Unknown action: ${action.type}`); } } const PreferencesContext = createContext(null); const PreferencesDispatchContext = createContext(null); export function PreferencesProvider({ children }) { const [prefs, dispatch] = useReducer(preferencesReducer, initialPreferences); return ( <PreferencesContext value={prefs}> <PreferencesDispatchContext value={dispatch}>{children}</PreferencesDispatchContext> </PreferencesContext> ); } export function usePreferences() { const prefs = useContext(PreferencesContext); if (prefs === null) throw new Error("usePreferences must be used inside <PreferencesProvider>."); return prefs; } export function usePreferencesDispatch() { const dispatch = useContext(PreferencesDispatchContext); if (dispatch === null) throw new Error("usePreferencesDispatch must be used inside <PreferencesProvider>."); return dispatch; } import { usePreferences, usePreferencesDispatch } from "./Preferences.jsx"; export default function Toolbar() { const prefs = usePreferences(); const dispatch = usePreferencesDispatch(); return ( <div className="toolbar"> <label> <input type="checkbox" checked={prefs.showMinutes} onChange={() => dispatch({ type: "minutes_toggled" })} /> Show minutes </label> <label> <input type="checkbox" checked={prefs.compact} onChange={() => dispatch({ type: "compact_toggled" })} /> Compact </label> </div> ); } body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

Check yourself

  1. What does useContext return when there's no provider above the component?
  2. Why put state and dispatch in separate contexts?
  3. When is composition a better fix for prop drilling than context?

Was this article helpful?

Your response is saved on this device.