Why useState Instead of Plain Variables in React
Understanding how React persists state across re-renders and why direct variable mutation fails to trigger UI updates.
Why useState Instead of Plain Variables in React
The Problem: Plain Variables Don't Survive Re-Renders
Have you ever wondered why we have to use useState? Why not just use plain variables to keep track of state in a functional component?
Let's say you have a todo list app. Instead of writing:
TypeScript
const [list, setList] = useState([]);
Why not just do this?
TypeScript
let list = [];
// When we need to update the list, we can just do:
list = newList;
The reason this doesn't work is what we'll cover in this article.
If you use let list = []; in your React application, you'll run into a couple of issues. First, when you add a todo item, the UI won't reflect the newly added item because reassigning a plain variable doesn't trigger React to re-render. Second, even if your component did re-render, the variable would reset to its initial value, wiping out whatever data it was holding. That's why React introduced useState.
What Is useState?
useState is a built-in React hook that adds state to a function component. It returns an array containing the current state value and a setter function to update it. When you call the setter with a new value, the component re-renders.
useState triggers a UI update when your state changes and persists your data across re-renders. For example, if your list has two items and the component re-renders, those two items will still be there. By contrast, if you used a plain variable like let name = "Jean", you would lose that value on re-render and it would reset to "initial value".
Keep two main things in mind:
useStatepersists data across re-renders.State updates trigger a re-render.
How useState Works
queue → compute → compare → re-render
Suppose you write:
TypeScript
const [name, setName] = useState("Jimmy");
React takes the initial value and stores it internally. Later, when you call setName("Peter"), React doesn't update the state immediately—instead, it adds the update to a queue and schedules a render.
During the next render, React processes the queue to compute the next value. If the new value is the same as the previous one in React's store, React skips re-rendering; otherwise, it re-renders. To do that comparison, React uses Object.is().
Because Object.is() checks reference and value equality rather than deep equality for objects and arrays, mutating state directly in place (like list.push(newItem)) won't give React a new reference—which is why mutating state doesn't trigger an update.
Todo List State Updates
Let's say you have a todo app. When the user adds a new item, you call addItem. The best way to do this is to create a new array and pass it to the state setter. Creating a new array gives React a new reference, so Object.is() sees that the state changed and triggers a re-render:
TypeScript
const addItem = (item: TodoItem) => {
setList([...list, item]);
};
Updating state in place like this would be incorrect:
TypeScript
const addItem = (item: TodoItem) => {
list.push(item);
};
This won't trigger a UI update because list still points to the same array reference. React compares the old and new references, sees no difference, and skips re-rendering.
Recap
Plain variables don't trigger re-renders and reset on every render;
useStatepersists data and schedules UI updates.State updates are queued and compared with
Object.is()before re-rendering.Always return new arrays or objects instead of mutating existing state in place.
A common mistake developers make is mutating an object or array instead of creating a new one when updating state. Understanding how Object.is() and re-renders work will save you a lot of debugging time.