Skip to content

FlashcardNot attempted

Typing useState that starts empty

Recall it before you reveal

How do you type state for data that is still loading?

Space to reveal
Show answer & explanation

Give the generic explicitly with null (or undefined) in the union: useState<User[] | null>(null), then narrow before rendering (if (!data) return <Spinner />).

Without the generic, TS infers null and you can never set real data. For complex state with several related fields, prefer a discriminated union ({ status: 'loading' } | { status: 'done'; data: User[] }) so impossible states can't be represented.