Context with a null default and a guard hook
Header destructures useAuth() directly: the guard hook's return type has no null. user itself can still be null (nobody logged in), and that is a real state the UI handles. <AuthContext value={value}> is the React 19 provider syntax; <AuthContext.Provider> is the older spelling.
Create context as createContext<Value | null>(null) and read it through a guard hook that throws outside the provider, so callers never see null; return tuples from custom hooks as const, and make hooks generic when the caller decides the type.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Create a typed CartContext with items: string[], add(item: string) and count: number, a CartProvider, and a guard hook useCart. Show the count in a header and an Add button in another component.
Write a generic hook useList<T>(initial: T[]) that returns { items, add, removeAt }, and use it with numbers and with objects.
Explain it without notes
Why use createContext<Value | null>(null) instead of createContext({} as Value)?
What does a guard hook do, and how does it change the type callers see?
Why does a custom hook returning [value, setter] need as const?
How does TypeScript decide T in a generic hook like useStoredState<T>(key, initial)?
Preview
Console
Nothing logged yet.Expected console output
Header sees: nobody
Header sees: Asha (admin)
Header sees: nobody