Skip to the page

TypeScript in real projects · core

Typing React components and hooks

Type props as an object type, children as React.ReactNode, events with React's event types, refs with useRef<HTMLDivElement>(null), and make reusable components generic when their data type varies.

The notes

What you must know

  • Props: type Props = { title: string; onSelect?: (id: string) => void; children?: React.ReactNode }. Avoid React.FC for new code; type the props parameter.
  • Events: React.ChangeEvent<HTMLInputElement>, React.MouseEvent<HTMLButtonElement>; or type the handler React.ChangeEventHandler<HTMLInputElement>.
  • useState with null start: useState<User | null>(null). useRef for DOM: useRef<HTMLInputElement>(null). In React 19, ref is a normal prop: ref?: React.Ref<HTMLButtonElement>.
  • Extend native props: React.ComponentProps<'button'> & { variant: 'primary' | 'ghost' }.
  • Generic component: function List<T>({ items, render }: { items: T[]; render: (x: T) => React.ReactNode }).
  • Custom hook returning a tuple: add as const so it isn't widened to an array of unions.

The red pen

Common traps: mistake → fix

  • Mistake: "React.FC is required"

    Fix: it is optional; plain functions with typed props are the common style now.

  • Mistake: "forwardRef is needed for refs"

    Fix: React 19 passes ref as a prop to function components.

Easy to hard

Questions interviewers ask, easy to hard

  1. How do you type a Button that accepts all native button props?
  2. How do you type a generic Table or List component?
  3. How do you type a custom hook's return value?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview