Courses / React 19: From Components to Production / Chapter 5
Module 2 · React Foundations · Chapter 5 of 30
State and Effects: useState, Events, useEffect and Cleanup
Builds on Components: Props, Children, Conditions, Lists and Keys. Next up: React Router: Routes, Layouts, Loaders and Navigation.
Part 1 · Responding to events
Handlers are functions you pass
A chai stall's shared bill changes each time someone adds a cup, and a train status panel must always show the answer for the train a rider picked. Both come down to state that React remembers and effects that keep the screen in step with the outside world.
Start with the input side. An event handler is a function that React calls when the user does something: a click, a key press, a form submission. You attach it by passing the function as a prop with a camelCase name such as onClick, onChange or onSubmit. On the stall's order screen, the handler of the Add cup button is where the cup count will change.
React calls the handler with one argument, an event object that wraps the browser's own event. It has the familiar methods preventDefault() and stopPropagation() and fields such as target. You rarely need more than that.
| Prop | Runs when |
|---|---|
onClick | the element is clicked, tapped or activated by keyboard |
onChange |