Sub topic 4
Updating state from event handlers
This chapter assumes you can write a function component and have declared state with useState. It also assumes you know that onClick and similar props accept functions.
Handlers are where state usually changes
An event handler is a function that React calls when the user does something, such as clicking, typing, or submitting a form. You pass it to an element as a prop. Inside the handler, you call the state setter. The setter does not change the variable immediately. It tells React to re-render the component with the new value.
import { useState } from 'react';
function Toggle() {
const [on, setOn] = useState(false);
function handleClick(e) {
console.log(e.type); // "click"
setOn(!on);
}
return <button onClick={handleClick}>{on ? 'On' : 'Off'}</button>;
}React passes a synthetic event object, here e, as the first argument. It has the same basic shape as a browser event, with properties such as type, target, and methods such as preventDefault().
Pass the function, do not call it
The most common handler mistake is calling the function inside JSX instead of passing it.
// Wrong: runs during render, not on click
<button onClick={handleClick()}>Toggle</button>
// Right: passes the function itself
<button onClick={handleClick}>Toggle</button>If handleClick() sets state, the wrong version triggers a render, which calls the handler again, which triggers another render. React stops this with a "Too many re-renders" error.
When a handler needs an argument, wrap the call in an arrow function:
<button onClick={() => handleSelect(item.id)}>Select</button>The arrow function is what React stores. It calls handleSelect only when the click happens.
Where to put the logic
Short logic can go inline, as in onClick={() => setCount(count + 1)}. Move it into a named function when the handler does more than one thing or when the JSX gets hard to read. Both styles behave identically. Handlers defined inside the component can read its current props and state directly.
What the handler sees
The setter schedules an update and does not change count for the rest of the running handler.
function handleClick() {
setCount(count + 1);
console.log(count); // still the old value
}The variable count belongs to the current render, so it stays fixed until the next one. The next chapter covers this behavior in detail.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| "Too many re-renders" | Handler called during render | Pass `handleClick`, not `handleClick()` |
| Handler fires on load | Same as above | Use an arrow function for arguments |
| Log shows old value after setter | State is a snapshot | Use the new value directly in the handler |
| Page reloads on submit | Default form behavior | Call `e.preventDefault()` |
Exercise
This button sets state, but the page reports "Too many re-renders". What is wrong, and how do you fix it? <button onClick={setOpen(true)}>Open</button>
Show answer
setOpen(true) runs during render, which queues another render, and so on. Pass a function instead: <button onClick={() => setOpen(true)}>Open</button>.