React as Snapshot

React.jpeg
Published on
/5 mins read

State as a Snapshot

In React, state is how you store mutable data within a component—for example, button click counts, input field contents, or a list of articles. However, React does not treat state as a "live" value that changes instantly. Instead, each time a component renders, state is considered a snapshot—a fixed record at the moment of that render.

Simply put:

  • When you call useState, React gives you a state value and a function to update it (setState).
  • The state value in the current render is a "snapshot" of the state at that moment.
  • When you call setState, React does not change the state immediately in the current render but schedules an update for the next render.

How State Works as a Snapshot

Basic Example

import { useState } from 'react'
 
function Counter() {
  const [count, setCount] = useState(0)
 
  function handleClick() {
    setCount(count + 1)
    console.log(count) // Still logs the old value!
  }
 
  return <button onClick={handleClick}>Count: {count}</button>
}
  • Initially, count = 0.
  • When the button is clicked:
    • setCount(count + 1) is called, but console.log(count) still logs 0.
    • React schedules a re-render with count = 1, and only in the next render will you see "Count: 1".

Why is that?

In the current render, count is a fixed snapshot (0). Calling setCount does not change that snapshot immediately. Instead, React waits until the next render to apply the new value.

Comparison with a Regular Variable

If you don't use state:

function Counter() {
  let count = 0
 
  function handleClick() {
    count = count + 1
    console.log(count) // Logs 1
  }
 
  return <button onClick={handleClick}>Count: {count}</button>
}
  • Clicking the button increases count to 1, but the UI still shows "Count: 0".
  • Reason: The variable count changes, but React does not know it needs to re-render.
  • State (useState) is how React tracks changes and updates the UI.

With state, React manages snapshots and automatically updates the UI when state changes.

Why is State as a Snapshot Important?

Predictability:

State is fixed in a single render, helping you understand exactly what value is being used. There are no unexpected mid-render changes.

Performance:

React batches state updates and processes them in the next render instead of updating little by little, optimizing speed.

Easier Debugging:

Since state is a snapshot, you can track each render's state value instead of guessing at changes.

Real-World Example: A News Website

1. Article View Counter

function ArticleView() {
  const [views, setViews] = useState(0)
 
  function handleView() {
    setViews(views + 1)
    setViews(views + 1) // Trying to increase it again
    console.log(views) // Still logs the old value!
  }
 
  return (
    <div>
      <button onClick={handleView}>View Article</button>
      <p>Views: {views}</p>
    </div>
  )
}
  • Initially, views = 0.
  • Clicking the button:
    • Calls setViews(views + 1) (expected 1).
    • Calls setViews(views + 1) again (expected 2).
    • But views in this render is still 0, and in the next render, it will only be 1.

Why not 2?

  • views is a snapshot (0) in the current render. Both setViews calls use 0, and React batches them into one update.
  • To update correctly, use the function updater:
function handleView() {
  setViews((currentViews) => currentViews + 1) // 0 -> 1
  setViews((currentViews) => currentViews + 1) // 1 -> 2
}
  • The function (currentViews) => ... retrieves the latest state each time it's called.

2. Search Bar for News

function SearchBar() {
  const [query, setQuery] = useState('')
 
  function handleChange(e) {
    setQuery(e.target.value)
    console.log(query) // Logs the old value
  }
 
  return (
    <div>
      <input value={query} onChange={handleChange} placeholder="Search..." />
      <p>You are searching for: {query}</p>
    </div>
  )
}
  • Typing "News":
    • setQuery("News") is called.
    • console.log(query) still logs "" (old snapshot).
    • In the next render, query will be "News", and the UI updates.

Key insight: State does not change immediately in the event handler but waits for the next render.

Important Notes

1. State is not updated immediately:

Calling setState is like scheduling: "Next time, render with this value." Don’t expect an immediate change in the same function.

2. Use function updates when depending on previous state:

If updating state based on the current value (like a counter), use setState(prev => prev + 1) instead of setState(state + 1).

3. Re-rendering is key:

State is a snapshot, and the UI updates only when React re-renders the component with the new snapshot.

Summary

"State as a Snapshot" means that in React, state works as a fixed snapshot at each render. When you update state using setState, React does not change the current snapshot but schedules a re-render with the new snapshot. This makes state predictable, improves performance, and prevents unexpected bugs.

In a news website, this is evident in features like a view counter or search bar: state does not update immediately but only in the next render. Understanding this concept helps you write more accurate React code and better utilize its power.