useState in React

From the React Hooks cheat sheet · State & Updates · verified Nov 2026

useState

Local component state

javascript
// Basic state
const [count, setCount] = useState(0)
const [user, setUser] = useState({ name: 'John' })

// Update patterns
setCount(count + 1)                    // Direct update
setCount(prev => prev + 1)             // Based on previous
setUser({ ...user, name: 'Jane' })     // Object update
💡 State updates are batched automatically
⚡ Use function update for values based on previous state
⚠️ The state variable keeps its old value until the next render
📌 useState(() => init()) runs the initializer only on the first render

Continue with React Hooks

Save the full cheat sheet or work through every related task.

More React tasks

Back to the full React Hooks cheat sheet