State Management Introduction
1. What Is State Management?
State management is the process of storing, updating and managing data that changes over time in a React application. State determines what information is displayed on the screen and how the user interface responds to user interactions.
For example, a counter value, logged-in user information, shopping cart items and selected theme can all be managed through state.
2. Why Is State Management Important?
- Stores changing application data.
- Updates the user interface when data changes.
- Maintains user selections and input values.
- Helps share information between components.
- Makes complex applications easier to organize.
- Supports predictable application behavior.
3. Understanding State with an Example
Consider a counter application. The counter value changes when the user clicks the Increment or Decrement button. React state stores the current value and updates the displayed number.
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<h2>Counter: {count}</h2>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
<button onClick={() => setCount(count - 1)}>
Decrement
</button>
</div>
);
}
export default Counter;
useState(0) initializes
the counter to zero. The setCount() function updates
the state, and React renders the updated value.
4. Types of State in React
| State Type | Description | Example |
|---|---|---|
| Local State | Used within a particular component. | Counter value |
| Shared State | State shared between related components. | Selected product |
| Global State | Application-wide state accessible to multiple components. | Logged-in user |
| Server State | Data fetched and managed from a server. | Product list from an API |
| URL State | State represented in the URL. | Search query or page number |
5. Local State vs Global State
Local State
- Belongs to a component.
- Useful for component-specific behavior.
- Commonly managed with
useState.
Example: Show or hide a password field.
Global State
- Can be accessed by multiple components.
- Useful for shared application information.
- Can use Context or Redux Toolkit.
Example: Shopping cart shared across pages.
6. State Management Flow
7. Managing State with useState
The useState Hook is a built-in React Hook used
to add state to function components.
const [state, setState] = useState(initialValue);
- state: The current state value.
- setState: The function used to update the state.
- initialValue: The initial state value.
import { useState } from "react";
function Student() {
const [name, setName] = useState("Samadhan");
return (
<div>
<h2>Student Name: {name}</h2>
<button onClick={() => setName("Samiksha")}>
Change Name
</button>
</div>
);
}
export default Student;
8. When Should You Use Global State?
Not every value needs to be stored globally. Start with local state and move to shared or global state when multiple components need access to the same information.
- Authentication information used throughout the application.
- Shopping cart data shared across pages.
- Application-wide theme preferences.
- Shared filters or selections used by multiple components.
9. State Management Tools
| Tool | Purpose |
|---|---|
| useState | Manages local component state. |
| useReducer | Manages state through a reducer function. |
| Context API | Shares values across components without passing props through every intermediate level. |
| Redux Toolkit | Provides structured management for shared application state. |
| Server-state libraries | Help manage fetching, caching and synchronizing server data. |
10. Practice Tasks
- Create a counter component using
useState. - Add Increment and Decrement buttons.
- Create a component that displays a student's name.
- Add a button to change the student's name.
- Create a show/hide feature for a paragraph.
- Identify which values in a shopping application should be local or global state.
- Explain when Context API or Redux Toolkit may be useful.
Summary
State management is an important part of React development because it controls how changing data is stored and displayed. Local state is suitable for component-specific information, while shared and global state help multiple components access common data. React provides Hooks such as useState and useReducer, along with Context, to support different state management requirements.