0Pricing
React Academy · Lesson

Typing Events & Refs in TypeScript

Use React.ChangeEvent, React.MouseEvent, and RefObject generics for DOM interactions.

React Synthetic Events

React wraps native DOM events in SyntheticEvent. Each event type has a specific generic: React.ChangeEvent<HTMLInputElement>, React.MouseEvent<HTMLButtonElement>, etc.

Typing onChange

The most common event is onChange on inputs. Type its handler with React.ChangeEvent<HTMLInputElement>.

function SearchInput() {
  const [value, setValue] = useState('');

  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    setValue(e.target.value);
  };

  return <input value={value} onChange={handleChange} />;
}

All lessons in this course

  1. Typing Props & Component Return Types
  2. Typing Events & Refs in TypeScript
  3. Generic Components & Utility Types
  4. Typing Context & Custom Hooks
← Back to React Academy