State as a Snapshot – React
State variables might look like regular JavaScript variables that you can read and write to. However, state behaves more like a snapshot. Setting it does not change the state variable you already have, but instead triggers a re-render. You might think of your user interface as changing directly in response to the user event like a click. In React, it works a little differently from this mental model. On the previous page, you saw that setting state requests a re-render from React. This means that for an interface to react to the event, you need to update the state. In this example, when you press “send”, setIsSent(true) tells React to re-render the UI: Here’s what happens when you click the button: Let’s take a closer look at the relationship between state and rendering. “Rendering” means that React is calling your component, which is a function. The JSX you return from that function is like a snapshot of the UI in time. Its props, event handlers, and local variables were all calculate
Learn React Adding Interactivity Copy page Copy State as a Snapshot State variables might look like regular JavaScript variables that you can read and write to. However, state behaves more like a snapshot. Setting it does not change the state variable you already have, but instead triggers a re-render. You will learn How setting state triggers re-renders When and how state updates Why state does not update immediately after you set it How event handlers access a “snapshot” of the state Setting state triggers renders You might think of your user interface as changing directly in response to the
Explore this link on the map →saved by
related reading
- Queueing a Series of State Updates – Reactreact.dev
- Updating Objects in State – Reactreact.dev
- The Interactive Guide to Rendering in Reactui.dev
- Updating Arrays in State – Reactreact.dev
- You Might Not Need an Effect – Reactreact.dev
- Blogged Answers: A (Mostly) Complete Guide to React Rendering Behavior · Mark's Dev Blogblog.isquaredsoftware.com
- Tutorial: Tic-Tac-Toe – Reactreactjs.org
- Why React Re-Renders • Josh W. Comeaujoshwcomeau.com
- React graph viscrubier.github.io
- When does React re-render components? | Felix Gerschaufelixgerschau.com
- Implementing React from scratchrob.directory
- Render and Commit – Reactreact.dev