useState Hook
Learn how to use the useState Hook to store, manage, and update data in React functional components.
1. What Is the useState Hook?
The useState Hook is a built-in React Hook that allows functional components to store and update state. State represents information that can change while a component is being used.
For example, a counter value, a student's name, a selected course, or a form input value can be stored using state. When state changes, React updates the component's UI when necessary.
useState from React before using it
in your component.
2. Syntax of useState
The basic syntax of the useState Hook is:
const [state, setState] = useState(initialValue);
Here is what each part means:
- state: The current state value.
- setState: The function used to update the state value.
- useState(initialValue): Initializes the state with the given value.
3. How useState Works
The state update process follows these steps:
React initializes the state when the component is rendered. When an event handler calls the setter function, React schedules an update and renders the component with the updated state.
4. Example: Counter Using useState
In this example, the counter starts at zero. Clicking the button increases the count by one.
Example Code
import { useState } from "react";
function App() {
const [count, setCount] = useState(0);
function increaseCount() {
setCount(count + 1);
}
return (
<div>
<h2>Counter Application</h2>
<p>Count: {count}</p>
<button onClick={increaseCount}>
Increase
</button>
</div>
);
}
export default App;
Expected Output
Counter Application
Initial value: Count: 0
After clicking Increase once: Count: 1
After clicking Increase again: Count: 2
useState(0) statement initializes the
counter to zero. The setCount() function
updates the counter, and React renders the new value.
5. Example: Updating a String State
The useState Hook can also store text values, such as a student's name.
Example Code
import { useState } from "react";
function App() {
const [name, setName] = useState("Samadhan");
function changeName() {
setName("Samiksha");
}
return (
<div>
<h2>Student Name</h2>
<p>Name: {name}</p>
<button onClick={changeName}>
Change Name
</button>
</div>
);
}
export default App;
Expected Output
Before clicking the button:
Name: SamadhanAfter clicking Change Name:
Name: SamikshasetName("Samiksha") updates the
state and React displays the updated name.
6. Using Multiple State Variables
A component can use the useState Hook multiple times to manage different values independently.
Example Code
import { useState } from "react";
function App() {
const [name, setName] = useState("Samadhan");
const [course, setCourse] = useState("React");
return (
<div>
<h2>Student Information</h2>
<p>Name: {name}</p>
<p>Course: {course}</p>
<button onClick={() => setName("Samiksha")}>
Change Name
</button>
<button onClick={() => setCourse("Angular")}>
Change Course
</button>
</div>
);
}
export default App;
Expected Output
Initially:
Name: SamadhanCourse: React
After clicking Change Name:
Name: SamikshaAfter clicking Change Course:
Course: Angularname and course. Each setter
updates its corresponding state value.
7. Important Points About useState
- Import useState from React before using it.
- Call useState at the top level of a functional component or custom Hook.
- Do not call Hooks inside loops, conditions, or nested functions.
- Use the setter function to update state instead of directly modifying the state variable.
- State can store strings, numbers, booleans, arrays, objects, and other JavaScript values.
- State updates may be batched, so the updated value should not be expected immediately in the same executing line.
setCount(previousCount => previousCount + 1).
8. Practice Tasks
Complete these exercises to practise the useState Hook:
- Create a counter that starts at 0 and increases by 1 when a button is clicked.
- Create a counter with Increase and Decrease buttons.
- Store a student's name in state and change it when a button is clicked.
- Create a component that switches a message between "Hello" and "Welcome".
- Store a course name and student name using separate state variables.
- Create a button that toggles a boolean state between true and false.