Choose provider boundaries
Choose how far a value should travel
A provider can sit around the whole application or just the feature that needs its setting. Start with explicit props for direct dependencies; composition can also remove unnecessary forwarding. Context becomes useful when multiple distant consumers need the same ambient value. A provider does not change siblings or ancestors.
import React, { createContext, useContext } from 'react';const DensityContext = createContext('comfortable');function Room({name}) { return <p>{name}: {useContext(DensityContext)}</p>; }export default function App() { return <main><DensityContext value="compact"> <Room name="East" /> <DensityContext value="comfortable"><Room name="Quiet" /></DensityContext> <Room name="West" /> </DensityContext></main>;}East and West show compact; Quiet shows comfortable because its nearer provider overrides the outer value. Removing the inner provider makes Quiet inherit compact. Place providers at the smallest common ancestor appropriate to the feature. Moving a provider may also change component identity if the tree structure changes, so verify state preservation when reorganizing a real app.
Require a provider when no default makes sense
import React, { createContext, useContext } from 'react';const RoomContext = createContext(null);function requireRoom(room) { if (room === null || room === undefined) throw new Error('Room provider is required'); return room;}function useRoom() { return requireRoom(useContext(RoomContext)); }function RoomLabel() { const room = useRoom(); return <p>Room: {room.name}</p>; }export default function App() { return <main><RoomContext value={{name:'Atlas'}}><RoomLabel /></RoomContext></main>;}The label reads Atlas. The small useRoom wrapper is a custom Hook (preview of Chapter 19); it reads context unconditionally and checks the value. In this application null and undefined are invalid by design. The guard deliberately throws if a required provider is missing, rather than silently showing fictional settings. A production error boundary may handle that error. Context is not an authorization boundary: the server must enforce access to protected data.
A provider returned by the reader is too late
import React, { createContext, useContext } from 'react';const RoomContext = createContext('Lobby');export default function App() { const room = useContext(RoomContext); return <RoomContext value="Atlas"><main><p>Room: {room}</p></main></RoomContext>;}This is valid JSX but it is incorrect if the intended label is Atlas. App reads its ancestors, not a provider it returns. Without another provider above App the label reads Lobby. Extract a child component that calls useContext and place that child inside the returned provider. If an apparently correct tree still fails, check whether import paths produce duplicate copies of the context object. Practice: draw the provider and consumer tree for the East, Quiet and West example and identify the reader that would see the fallback if moved outside.