What Is State in React?
Learn how React state stores changing information, remembers values between renders, and updates the user interface when users interact with an application.
1. What Is State?
State is information that a React component remembers between renders. It can represent a counter value, a student's name, a selected course, or whether a menu is open or closed.
When a user performs an action, a component can request a state update. React then renders the component again when necessary to display the updated information.
Simple example:
Imagine a CIIT Training Institute student dashboard. The dashboard displays the number of courses completed by a student. When a course is completed, that number can change from 2 to 3.
2. Why Do We Need State?
Websites often need to respond to user interactions. For example, a counter must display a new number after a button click, and a form must remember the text entered by a student.
A normal local variable does not itself tell React to render the component again. React state provides a way to preserve information between renders and request UI updates when the value changes.
| Application Feature | State Example |
|---|---|
| Counter | Current number |
| Student registration | Entered name and form values |
| Course dashboard | Selected course or progress |
| Navigation menu | Open or closed status |
| Search interface | Current search text |
3. Understanding State with an Example
Let's create a simple counter. Its initial value is 0. Every time the user clicks the Increase button, React updates the state and displays the new count.
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<h2>Count: {count}</h2>
<button onClick={() => setCount(count + 1)}>
Increase
</button>
</div>
);
}
export default Counter;
Code explanation:
-
useState(0)declares state with an initial value of 0. -
countrepresents the current state value for that render. -
setCountrequests an update to the count. -
onClickcalls the setter when the button is clicked. -
{count}displays the current count in JSX.
Expected Browser Output
Initial display: Count: 0
After one click: Count: 1
After two clicks: Count: 2
4. How State Updates the UI
State connects user interactions with the information displayed by a React component. The following diagram shows the basic flow.
Step-by-step explanation:
- The user clicks the Increase button.
- The button's event handler runs.
- The state setter requests a new count value.
- React renders the component with the new state.
- The screen displays the updated count.
5. State and Normal Variables
JavaScript variables and React state serve different purposes. A normal local variable is recreated when the component function runs again. Changing that variable does not itself trigger a React render.
function Example() {
let count = 0;
function increase() {
count = count + 1;
console.log(count);
}
return (
<button onClick={increase}>
Increase
</button>
);
}
export default Example;
In this example, the variable changes when the handler runs, but that change does not itself make React render the new value on the screen.
Now compare it with React state:
import { useState } from "react";
function Example() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Count: {count}
</button>
);
}
export default Example;
| Normal Variable | React State |
|---|---|
| Changing it does not itself trigger a render. | Setter updates can schedule a render. |
| A local variable is recreated when the function runs again. | React preserves state between renders. |
| Useful for temporary calculations. | Useful for changing values displayed in the UI. |
6. State Can Store Different Types of Data
React state is not limited to numbers. It can store strings, Boolean values, arrays, objects, and other JavaScript values.
import { useState } from "react";
function StudentDashboard() {
const [studentName, setStudentName] = useState("Rahul");
const [isEnrolled, setIsEnrolled] = useState(true);
const [completedCourses, setCompletedCourses] = useState(2);
return (
<div>
<p>Student: {studentName}</p>
<p>Enrolled: {isEnrolled ? "Yes" : "No"}</p>
<p>Completed Courses: {completedCourses}</p>
</div>
);
}
export default StudentDashboard;
| State Variable | Data Type | Initial Value |
|---|---|---|
| studentName | String | Rahul |
| isEnrolled | Boolean | true |
| completedCourses | Number | 2 |
Expected Browser Output
Student: Rahul
Enrolled: Yes
Completed Courses: 2
7. Important Rules of State
- Use the state setter to request an update.
- Do not directly mutate state objects or arrays.
- Call Hooks at the top level of a function component or custom Hook.
- State updates may be batched by React.
- Use functional updates when the next state depends on the previous state.
- Treat state values as read-only.
8. Common Mistakes
Mistake 1: Updating a state variable directly
count = count + 1;
Direct assignment does not request a React state update. Use the setter instead.
setCount(count + 1);
Mistake 2: Expecting an immediate state change
A state setter requests an update. Reading the same state variable immediately afterward in the current handler still gives the value from that render.
Mistake 3: Calling Hooks conditionally
Do not call useState inside an if statement, loop, or nested function. Call it at the top level of your component.
9. Practice Exercises
Try the following exercises in your React project:
- Create a counter that starts at 0.
- Add Increase and Decrease buttons.
- Create a button that shows or hides a message.
- Store a student's name in state.
- Display the number of completed courses.
- Explain why a state setter can update the UI.
10. Quick Revision
- State stores information that a component remembers.
- State can contain numbers, strings, Boolean values, arrays, and objects.
- useState is a Hook used to declare state.
- The setter requests a state update.
- React can render the component using the updated state.
- State makes interactive user interfaces possible.
Summary
React state allows components to remember information between renders. It is useful for counters, student forms, course dashboards, menus, and other interactive features. The useState Hook provides the current state value and a setter function. When an update is requested, React can render the component again and display the updated information. In the next lesson, we will learn how to use the useState Hook in detail.