Event Handler
In one line
An event handler is a function that runs in response to a user action (click, type, submit) — attached to elements via props like onClick.
In simple words
In React, you handle user events by passing a function as a prop named after the event: onClick, onChange, onSubmit, onMouseOver, etc. When the event fires, React calls your function with a synthetic event object.
React's synthetic events wrap the browser's native events to provide a consistent API across browsers. They have the same interface (e.target, e.preventDefault(), e.stopPropagation()) but are pooled for performance — in older React versions you had to call e.persist() to use them asynchronously.
A common pattern is to define the handler inline or as a method and pass it to the element. For performance, you may wrap handlers in useCallback to avoid creating new function instances on every render.
Example
function Form() {
const [text, setText] = useState("");
return (
<input
value={text}
onChange={(e) => setText(e.target.value)}
/>
);
}onChange fires on every keystroke. The handler reads e.target.value (the input's current text) and updates state.
Related terms
Related Resources
Related Lessons
- Event Handling
Learn React event handlers and synthetic events.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.