Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

In React, use useState to remember a value between renders and update the interface when that value changes. Vite provides the project’s development tooling; React’s state setter is what requests the component update.

What does useState do?

A component often needs to remember information that can change, such as a count, the text in an input, the selected tab, or whether a panel is open. React calls this component-specific memory state. The useState Hook gives a component its current state value and a setter function to request an update.

Import the Hook from React and call it at the top level of your component:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import { useState } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);

  return (
    <button onClick={() => setCount(count + 1)}>
      Count: {count}
    </button>
  );
}

Here, count is the current value, setCount is its setter, and 0 is the initial value. When the button is clicked, the setter requests an update. React renders the component again, and the button displays the new count. See React’s State: A Component’s Memory guide.

Why isn’t a regular variable enough?

A variable declared inside a component function is created again each time that function runs. Changing it does not preserve the value for a later render, and it does not tell React that the interface needs to be rendered again.

Approach Persists between renders? Requests a render when changed? Can update displayed JSX?
Local variable, such as let count = 0 No; it is recreated when the component runs again. No. Not by changing the variable alone.
State declared with useState Yes, for that component instance. Yes, when its setter is called. Yes; React renders again using the updated state.

That is why a changing value that should affect the interface belongs in state rather than in a regular local variable.

What happens when you call the setter?

Setting state requests a new render; it does not rewrite the state variable in code that is already running. An event handler sees the state snapshot from the render that created it. For example, after calling setCount(count + 1), reading count again in that same handler still gives the value from that render. React uses the requested update when it renders the component again. React explains this in State as a Snapshot.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Where should a state Hook go?

Call useState at the top level of a function component or custom Hook. Do not put it inside a condition, loop, event handler, or nested function. Keeping Hook calls at the top level lets React associate state with the component consistently.

Does each component share the same state?

No. State belongs to a particular component instance, so two instances of the same component have separate local state. If multiple components need to coordinate around one value, move that state to their closest common parent and pass the value and any needed event handlers down. React calls this sharing state between components; its guide explains the pattern in Sharing State Between Components.

How should you update object state?

When state holds an object, create a replacement object for an update rather than mutating the existing object and passing that same object back. For example, if a component stores form in state, an update can use a spread to retain its other fields while replacing one:

setForm({ ...form, name: 'Ada' });

React’s guidance on updating objects in state covers this approach.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What role does Vite play?

Vite is development tooling for the project, not a state manager. Its Getting Started guide describes Vite’s command-line tool, the project’s index.html entry point, and a manual setup example served at http://localhost:5173. React’s setter—not Vite—requests the component’s state update. Consult the current Vite Getting Started guide and React installation instructions for project setup; the exact scaffold command and configuration depend on the setup you choose.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.