Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Routing and Application Architecture
Stage 5: Specialization (pick ONE)

Part 09 / Topic 4 of 6

createContext's default value: a fallback for an unwired tree, not a starting state

createContext(defaultValue)'s argument is easy to misread as "what the Provider starts with." It is not - a Provider's value prop always wins the moment one exists above a consumer. The default is only ever used by a consumer that renders with no matching Provider anywhere above it in the tree. examples/04_default_value_without_provider.jsx proves both halves of that rule:

// examples/04_default_value_without_provider.jsx
export const CurrencyContext = createContext("₹");

export function PriceTag({ amount }) {
  const currencySymbol = useContext(CurrencyContext);
  return <p>{currencySymbol}{amount}</p>;
}

Rendering a bare <PriceTag amount={499} />, with no CurrencyContext.Provider anywhere above it, really shows "₹499" - createContext's default, not a crash and not undefined (examples/04_default_value_without_provider.test.jsx). Rendering <PriceTagWithProvider amount={499} currencySymbol="$" />, which wraps the exact same PriceTag in a Provider with value="$", really shows "$499" instead - the nearest Provider overrides the default completely, every time one exists.

Done here? The practice set for Part 9 — including the solutions — is on the part page.

Search the guide