State is data stored inside a component that can change over time. When state changes, React re-renders the component so the UI shows the latest value. State is what makes React interfaces interactive.
Examples of state include a counter value, search text, whether a modal is open, form input values, selected tabs, and fetched data that must be displayed on the page.
| Feature | Props | State |
|---|---|---|
| Where it comes from | Passed from parent | Stored inside the component |
| Can it change? | Not by the child | Yes, through a state setter |
| Main purpose | Receive external input | Store changing local data |
The useState hook adds state to function components. It returns two values: the current state and a function used to update that state.
import { useState } from 'react'
function Counter() {
const [count, setCount] = useState(0)
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increase</button>
</div>
)
}When the next state depends on the previous state, use the functional form of the setter. This avoids stale values during rapid updates.
setCount(previousCount => previousCount + 1)A component can have more than one piece of state. Each piece should represent one meaningful changing value.
const [name, setName] = useState('')
const [age, setAge] = useState(0)
const [isOnline, setIsOnline] = useState(false)When state is an object, remember to copy the old object before changing a single field. Unlike class component state, React does not merge object state automatically in function components.
const [user, setUser] = useState({ name: 'Aman', city: 'Delhi' })
function changeCity() {
setUser(current => ({
...current,
city: 'Mumbai'
}))
}Arrays in state should also be updated immutably. Instead of changing the existing array directly, create a new array with methods such as map, filter, or the spread operator.
const [items, setItems] = useState(['HTML', 'CSS'])
function addItem() {
setItems(current => [...current, 'React'])
}
function removeItem(itemToRemove) {
setItems(current => current.filter(item => item !== itemToRemove))
}useStateA value should usually be state if it changes over time and its change should update the UI. If a value never changes or can be calculated directly from existing props and state, it may not need its own state.
| Mistake | Why it is a problem | Better approach |
|---|---|---|
| Changing state directly | React may not update correctly | Always use the setter function |
| Storing derived values as extra state | Creates duplication and sync bugs | Compute derived values during render when possible |
| Putting too much unrelated data in one state object | Makes updates harder to reason about | Split state by concern when it improves clarity |
| Forgetting to copy arrays or objects | Mutates existing state and breaks predictability | Use spread, map, filter, or structured updates |
State is what makes React components dynamic. It allows components to respond to user input, fetched data, and application events. Once you understand how to create, update, and organize state correctly, building interactive React interfaces becomes much easier and more predictable.
Explore 500+ free tutorials across 20+ languages and frameworks.