Variables vs. State in React.js: Why State Rules the UI ๐

If you've spent any time learning React.js, you've likely encountered the concept of state. Itโs one of the framework's fundamental building blocks, but it often brings up a basic question: Why can't I just use a regular JavaScript variable?
While a variable seems simpler, the difference between a plain variable and React's state is the key to building dynamic, responsive, and correct user interfaces. Let's break down why you need state in React.
๐งฑ The Problem with Plain Variables
Imagine you want a counter button in your React component. Every time you click it, the number should go up.
If you try to use a simple JavaScript variable inside a functional component:
function Counter() {
let count = 0; // A simple variable
const handleClick = () => {
count = count + 1; // ๐ซ The number changes in memory...
console.log(count); // ...and the console shows the update!
};
return (
<div>
<p>Count: {count}</p> {/* ๐ ...but the screen doesn't update! */}
<button onClick={handleClick}>Increment</button>
</div>
);
}
The issue here is Re-rendering.
When you click the button,
handleClickruns, andcountis updated in the component's memory.However, React has no idea that this variable change should prompt a visual update to the browser's DOM (Document Object Model).
The component doesn't re-run, the
returnJSX isn't executed again, and the screen stays the same.
A simple variable changes in memory, but it doesn't trigger the re-render mechanism that tells React to update the UI.
โจ Introducing State: The React Superpower
This is where State comes in. State, typically managed using the useState Hook in functional components, is essentially a special kind of variable that React actively tracks.
When you use useState, you get two things:
The current value (
count).A function to update that value (
setCount).
The Magic of the Setter Function
import React, { useState } from 'react';
function Counter() {
// โ
This is state!
const [count, setCount] = useState(0);
const handleClick = () => {
setCount(count + 1); // ๐ก The number changes AND it triggers a re-render!
};
return (
<div>
<p>Count: {count}</p> {/* Now this line gets re-executed! */}
<button onClick={handleClick}>Increment</button>
</div>
);
}
When you call the setter function (setCount):
React updates the state variable (
count) in its memory.Crucially, it marks the component as "dirty" or needing an update.
React then re-runs (re-renders) the component function.
The entire component function executes again with the new value of
count.React compares the new output (the updated JSX) with the old output and efficiently updates the DOM in the browser to show the new number.
The component's appearance is now synchronized with the data!
๐ Variables vs. State: A Quick Comparison
Feature | Simple JavaScript Variable ( | React State ( |
Persistence | Reset to its initial value on every component re-render. | Persisted (retained) by React between re-renders. |
Updates UI | No. Changes happen in memory only. | Yes. Changes trigger a component re-render. |
Primary Use | Holding temporary values used during one render cycle (e.g., a loop counter). | Holding data that changes over time and affects the UI (e.g., user input, fetched data, active tab). |
The Takeaway
Variables are great for storing values that are calculated and used within a single render cycle and do not affect the UI after that.
State is what you must use for any data that changes and whose change should be reflected visually on the user's screen.
By embracing useState, you are signaling to React: "Hey, this piece of data is important! If it changes, please stop what you're doing, re-render my component, and update the UI to match!" This mechanism is the very core of how React achieves its efficiency and declarative UI design. Happy coding!



