TypeScript
/Intermediate
React: Typing Events
Definition
Providing accurate types for Event Objects passed to event handler functions (like `onClick` or `onChange`) in React.
Explain Like I'm New
In vanilla JS, an event is just an `Event`. In React, events are wrapped in a 'SyntheticEvent'. If you have an `onChange` handler for an input, you must type the event so TypeScript knows it is allowed to read `event.target.value`.
Real World Example
An input handler: `const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { setValue(e.target.value); }`.
Common Use Cases
- •Form handling
- •Button clicks
- •Keyboard navigation
Interactive Example
Loading Code Playground...
Interview Questions
basic
- Do you need to type the event if the function is written completely inline?
intermediate
- What is the event type for an `<input>` onChange handler?
advanced
- What is the difference between `React.MouseEvent` and `MouseEvent`?