useState Hook in React
Learn how to declare state in functional components using the useState Hook. Understand initial values, state variables, setter functions, and practical examples for interactive React applications.
1. What Is the useState Hook?
The useState Hook is a built-in React Hook that allows a function component to store and update state.
It returns two values: the current state value and a setter function that requests an update to that value.
For example, a CIIT Training Institute student dashboard can use state to store the number of completed courses, the selected course, or the text entered into a form.
import { useState } from "react";
const [count, setCount] = useState(0);
2. Syntax of useState
The general syntax of the useState Hook is:
const [state, setState] = useState(initialValue);
| Part | Meaning |
|---|---|
| useState | The React Hook used to declare state. |
| state | The current state value for the current render. |
| setState | The function used to request a state update. |
| initialValue | The initial state value used when the component initializes. |
The names state and setState are examples. You can choose meaningful names such as count and setCount, or name and setName.
3. Importing useState
Before using the Hook, import it from React. You can then call it at the top level of your function component.
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<h2>Count: {count}</h2>
);
}
export default Counter;
Explanation:
-
import { useState } from "react"imports the Hook. -
useState(0)initializes the count to 0. -
countholds the current count value. -
setCountrequests a count update.
Expected Browser Output
Count: 0
4. How useState Works
When React renders a component, it provides the current state value. When the setter is called, React schedules an update and renders the component again when necessary.
Step-by-step:
- React renders the component.
- The component reads its current state value.
- A user performs an action, such as clicking a button.
- The setter requests a new state value.
- React renders the component with the updated value.
5. Example: Counter with useState
A counter is one of the easiest ways to understand useState. The initial count is 0. Every button click requests an increment of the count.
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
function increaseCount() {
setCount(count + 1);
}
return (
<div>
<h2>Counter: {count}</h2>
<button onClick={increaseCount}>
Increase
</button>
</div>
);
}
export default Counter;
How the code works:
- The counter starts at 0.
- The Increase button calls increaseCount.
- increaseCount requests the next count value.
- React renders the new count in the heading.
Expected Browser Output
Initially: Counter: 0
After one click: Counter: 1
After two clicks: Counter: 2
6. Different Initial Values
The useState Hook can store different JavaScript data types. The initial value determines the starting value for that state variable.
import { useState } from "react";
function StudentInfo() {
const [name, setName] = useState("Rahul");
const [age, setAge] = useState(20);
const [isActive, setIsActive] = useState(true);
return (
<div>
<p>Name: {name}</p>
<p>Age: {age}</p>
<p>Active: {isActive ? "Yes" : "No"}</p>
</div>
);
}
export default StudentInfo;
| State Variable | Initial Value | Type |
|---|---|---|
| name | "Rahul" | String |
| age | 20 | Number |
| isActive | true | Boolean |
Expected Browser Output
Name: Rahul
Age: 20
Active: Yes
7. Updating State with a Setter
A setter can receive the next state value directly. For example, setName("Amit") requests that the name state be updated to Amit.
import { useState } from "react";
function Student() {
const [name, setName] = useState("Rahul");
function changeName() {
setName("Amit");
}
return (
<div>
<p>Student Name: {name}</p>
<button onClick={changeName}>
Change Name
</button>
</div>
);
}
export default Student;
Expected Browser Output
Before clicking: Student Name: Rahul
After clicking: Student Name: Amit
8. Rules of the useState Hook
- Call useState at the top level of a function component or custom Hook.
- Do not call Hooks inside conditions, loops, or nested functions.
- Use the setter function instead of directly assigning a new value to state.
- Treat objects and arrays in state as immutable.
- Use a functional update when the next value depends on the previous value.
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
function increaseCount() {
setCount(previousCount => previousCount + 1);
}
return (
<button onClick={increaseCount}>
Count: {count}
</button>
);
}
export default Counter;
The functional updater receives the previous state value and calculates the next value from it. This is useful when an update depends on the previous state.
9. Common Mistakes
Mistake 1: Forgetting to import useState
If you use the named Hook without importing it, your component may fail to compile or run.
Mistake 2: Changing state directly
// Incorrect count = count + 1; // Correct setCount(count + 1); // Based on the previous value setCount(previousCount => previousCount + 1);
Mistake 3: Calling useState conditionally
if (isLoggedIn) {
const [count, setCount] = useState(0);
}
Call useState at the top level of the component instead so that Hook calls remain in the same order between renders.
10. Quick Practice
Try these exercises in your React project:
- Create a counter using useState.
- Change a student's name when a button is clicked.
- Store an age as a number in state.
- Use a Boolean state value to show or hide a message.
- Explain the difference between a state value and its setter.
Summary
The useState Hook lets function components store and update state. It returns the current state value and a setter function. The initial value is used when the component initializes, while setter calls request updates that React can render on the screen. useState can manage numbers, strings, Boolean values, and other JavaScript data. Following the Rules of Hooks and updating state correctly helps build reliable interactive applications.