Course contentsShow
← React
Lesson 2350 of 2,39651. TypeScript with React BasicsFree lesson

Event Handler Type Basics: React.MouseEvent and React.ChangeEvent

Learn the most common React event types and when to use MouseEvent, ChangeEvent, FormEvent, and KeyboardEvent.

Event Handler Type Basics: React.MouseEvent and React.ChangeEvent

You'll learn how to properly type the most common React event handlers using TypeScript's built-in React event types.

The Core Idea

When you write event handlers in React with TypeScript, you can't just use the browser's native MouseEvent or Event types—React wraps these in its own synthetic event system for cross-browser consistency. TypeScript provides specific types like React.MouseEvent and React.ChangeEvent that match React's wrapped versions.

Think of it like this: React events are gift-wrapped versions of native browser events. You need to tell TypeScript you're unwrapping React's wrapper, not the raw browser event.

The Most Common Event Types

React.MouseEvent — for clicks, hovers, mouse movements:

function Button() {
  const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
    console.log(event.clientX); // Mouse position works
  };

  return <button onClick={handleClick}>Click me</button>;
}

React.ChangeEvent — for input changes:

function SearchBox() {
  const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    console.log(event.target.value); // Input value is typed correctly
  };

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

React.FormEvent — for form submissions:

const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
  event.preventDefault();
};

React.KeyboardEvent — for keyboard interactions:

const handleKeyPress = (event: React.KeyboardEvent<HTMLInputElement>) => {
  if (event.key === 'Enter') { /* ... */ }
};

Notice the generic parameter (<HTMLButtonElement>, <HTMLInputElement>) — this tells TypeScript which specific DOM element triggered the event, giving you accurate autocomplete for element-specific properties.

Key Takeaway: Use React's synthetic event types (React.MouseEvent, React.ChangeEvent, etc.) with a generic parameter specifying the HTML element type to get proper TypeScript safety in event handlers.