Full answer
React events look like HTML events, with differences. Names are camelCase (onClick, onChange), and the value is a function, not a string: <button onClick={handleClick}>.
The first common mistake is calling the function. In this fence, the handler runs while rendering instead of on click (error on purpose):
function Row({ id, onDelete }) {
return <button onClick={onDelete(id)}>Delete</button>;
}onDelete(id) runs immediately during render and passes its result to onClick. The fix is a function that calls it later: onClick={() => onDelete(id)}. Without arguments, pass the name: onClick={handleClick}.
The handler receives a SyntheticEvent, React's wrapper that behaves the same in all browsers. It has the usual methods: preventDefault() to cancel default behaviour, such as a form submit reloading the page, and stopPropagation() to stop the event bubbling to parents. event.target is the element that triggered the event. event.currentTarget is the element the handler is attached to. Since React 17 events are attached to the root container, and the old event pooling is gone, so you can read the event after an await.
Handlers can update state, call APIs and navigate. Do not use an effect for logic that exists only because the user clicked, since the handler already knows which action happened.
Some events bubble in surprising ways. A click on a button inside a clickable card runs both handlers. Use stopPropagation on the inner one when that is not wanted.
The form below stops the default submit, reads the typed value from state and passes an id to a handler.
import { useState } from 'react';
export default function SearchForm() {
const [query, setQuery] = useState('');
const [searches, setSearches] = useState([]); // items look like { id, text }
function handleSubmit(event) {
event.preventDefault();
console.log('submitted ' + query);
setSearches((list) => [...list, { id: list.length ? list[list.length - 1].id + 1 : 1, text: query }]);
setQuery('');
}
function handleRemove(id) {
setSearches((list) => list.filter((item) => item.id !== id));
}
return (
<form onSubmit={handleSubmit}>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
<button type="submit">Search</button>
<ul>
{searches.map((item) => (
<li key={item.id}>
{item.text} <button type="button" onClick={() => handleRemove(item.id)}>Remove</button>
</li>
))}
</ul>
</form>
);
}The check typed a name and pressed Search. The page did not reload, the value was logged, and it was added to the list. Output of the run: submitted Asha Rendered result: <form><input value=""><button type="submit">Search</button><ul><li>Asha <button type="button">Remove</button></li></ul></form>
Key points
- Event props are camelCase and take a function.
- Pass the function or an arrow wrapper. Do not call it in JSX.
- preventDefault cancels default browser behaviour, and stopPropagation stops bubbling.
- event.target is the origin element and event.currentTarget is the element holding the handler.
- Logic caused by a user action belongs in the handler, not in an effect.
Common mistakes
- Writing onClick={handler()} and running the handler during render.
- Forgetting event.preventDefault() on a form submit.
- Confusing event.target with event.currentTarget when a child element is clicked.
- Using a string handler like onclick="..." from HTML.
- Calling a setter in the render body instead of in a handler.
Follow-ups
- What is event delegation in React, and where are listeners attached?
- How do you stop a click in a child from triggering the parent handler?
- When should you add a native event listener instead of a React prop?
Tips
- Mention the "do not call it" trap first, because it appears in many screening tests.
- Know target versus currentTarget. It is a standard follow-up.
- Say that handlers, not effects, are the place for reacting to user actions.