Create and consume context
Share a display preference
Context lets a component make a value available to descendants that need it, even when intermediate components do not use that value. Think of it as a named connection through the component tree, not a global variable. Declare the connection once at module scope, provide a value above consumers, and read it with a Hook. Each example is an independent App.jsx for the Chapter 3 client project; visual outcomes are expected, not browser-tested.
import React, { createContext, useContext } from 'react';const DensityContext = createContext('comfortable');function Shelf() { const density = useContext(DensityContext); return <p>Shelf spacing: {density}</p>;}function Room() { return <section><h2>Reading room</h2><Shelf /></section>; }export default function App() { return <main><DensityContext value="compact"><Room /></DensityContext></main>;}The shelf displays compact although Room does not receive or forward a density prop. React 19 supports the context object itself as the provider tag. For React 18, replace both provider tags with DensityContext.Provider. The context object identifies the connection; the provider supplies its value. A context declaration does not create mutable application state.
Update the value at its owner
import React, { createContext, useContext, useState } from 'react';const DensityContext = createContext('comfortable');function Shelf({name}) { return <p>{name}: {useContext(DensityContext)}</p>;}function nextDensity(density) { return density === 'comfortable' ? 'compact' : 'comfortable';}export default function App() { const [density, setDensity] = useState('comfortable'); return <main><h1>Shelf settings</h1> <button onClick={() => setDensity(nextDensity)}>Toggle spacing</button> <DensityContext.Provider value={density}> <Shelf name="Fiction" /><Shelf name="History" /> </DensityContext.Provider></main>;}Both shelves initially show comfortable and switch together after activation. The state belongs to App. The context shares the latest value; it does not supply a setter automatically. The updater is an ordinary pure function. This example uses .Provider, which also works in React 19.
Observe a fallback without a provider
import React, { createContext, useContext } from 'react';const UnitContext = createContext('pages');function Unit() { return <p>Reading unit: {useContext(UnitContext)}</p>; }export default function App() { return <main><Unit /> <UnitContext value="chapters"><Unit /></UnitContext></main>; }The first line reads pages; the second reads chapters. The fallback stays fixed and is used only when there is no corresponding ancestor provider. Passing undefined through an existing provider is different from omitting a provider. Use the same exported context object for readers and providers. The older Context.Consumer render-function interface remains relevant in legacy code; prefer useContext in new function components. Practice: predict what changes if the first Unit is moved inside the provider.