Adding Interactivity
Responding to events
React lets you add event handlers to your JSX. Event handlers are your own functions that will be triggered in response to user interactions like clicking, hovering, focusing on form inputs, and so on.
Built-in components like <button>
only support built-in browser events like onClick
. However, you can also create your own components, and give their event handler props any application-specific names that you like.
Ready to learn this topic?
Read to learn how to add event handlers.
State: a component’s memory
Components often need to change what’s on the screen as a result of an interaction. Typing into the form should update the input field, clicking “next” on an image carousel should change which image is displayed, clicking “buy” puts a product in the shopping cart. Components need to “remember” things: the current input value, the current image, the shopping cart. In React, this kind of component-specific memory is called state.
You can add state to a component with a Hook. Hooks are special functions that let your components use React features (state is one of those features). The useState
Hook lets you declare a state variable. It takes the initial state and returns a pair of values: the current state, and a state setter function that lets you update it.
Here is how an image gallery uses and updates state on click:
Ready to learn this topic?
Read to learn how to remember a value and update it on interaction.
Render and commit
Before your components are displayed on the screen, they must be rendered by React. Understanding the steps in this process will help you think about how your code executes and explain its behavior.
Imagine that your components are cooks in the kitchen, assembling tasty dishes from ingredients. In this scenario, React is the waiter who puts in requests from customers and brings them their orders. This process of requesting and serving UI has three steps:
- Triggering a render (delivering the diner’s order to the kitchen)
- Rendering the component (preparing the order in the kitchen)
- Committing to the DOM (placing the order on the table)
Trigger Render Commit
Illustrated by Rachel Lee Nabors
Ready to learn this topic?
Read to learn the lifecycle of a UI update.
State as a snapshot
Unlike regular JavaScript variables, React state behaves more like a snapshot. Setting it does not change the state variable you already have, but instead triggers a re-render. This can be surprising at first!
This behavior helps you avoid subtle bugs. Here is a little chat app. Try to guess what happens if you press “Send” first and then change the recipient to Bob. Whose name will appear in the alert
five seconds later?
Ready to learn this topic?
Read to learn why state appears “fixed” and unchanging inside the event handlers.
Queueing a series of state updates
This component is buggy: clicking “+3” increments the score only once.
explains why this is happening. Setting state requests a new re-render, but does not change it in the already running code. So score
continues to be 0
right after you call setScore(score + 1)
.
You can fix this by passing an updater function when setting state. Notice how replacing setScore(score + 1)
with setScore(s => s + 1)
fixes the “+3” button. This lets you queue multiple state updates.
Ready to learn this topic?
Read to learn how to queue a sequence of state updates.
Updating objects in state
State can hold any kind of JavaScript value, including objects. But you shouldn’t change objects and arrays that you hold in the React state directly. Instead, when you want to update an object and array, you need to create a new one (or make a copy of an existing one), and then update the state to use that copy.
Usually, you will use the ...
spread syntax to copy objects and arrays that you want to change. For example, updating a nested object could look like this:
If copying objects in code gets tedious, you can use a library like to reduce repetitive code:
Ready to learn this topic?
Read to learn how to update objects correctly.
Updating arrays in state
Arrays are another type of mutable JavaScript objects you can store in state and should treat as read-only. Just like with objects, when you want to update an array stored in state, you need to create a new one (or make a copy of an existing one), and then set state to use the new array:
If copying arrays in code gets tedious, you can use a library like to reduce repetitive code:
Ready to learn this topic?
Read to learn how to update arrays correctly.
What’s next?
Head over to to start reading this chapter page by page!
Or, if you’re already familiar with these topics, why not read about ?