Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 12.5 — Typing Context and Custom Hooks
React · Example 1 of 3 React 19+

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.

01

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.

02

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

01

Why use createContext<Value | null>(null) instead of createContext({} as Value)?

02

What does a guard hook do, and how does it change the type callers see?

03

Why does a custom hook returning [value, setter] need as const?

04

How does TypeScript decide T in a generic hook like useStoredState<T>(key, initial)?

Context with a null default and a guard hook React 19+

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

Header sees: nobody
Header sees: Asha (admin)
Header sees: nobody