Introduction to Redux
Learn how Redux helps React applications manage shared state in a predictable and organized way.
1. What Is Redux?
Redux is a state management library that helps applications manage shared data in a centralized store. Instead of passing the same data through many components, components can access the shared state through Redux.
Why Do We Need Redux?
In a large React application, many components may need the same data. Managing this data separately in different components can make the application difficult to maintain. Redux provides a structured way to manage this shared state.
Example
Imagine a training institute application. The Navbar displays the logged-in student's name, the Profile page displays the same student's details, and the Dashboard displays the student's course information. These components may need access to shared data.
Without Redux
Data may need to pass through multiple components using props.
With Redux
Components can access the shared state from the central store through the appropriate Redux bindings.
2. What Is a Redux Store?
A Redux Store is the central place where the application's Redux state is maintained. It holds the data that needs to be shared across components.
Example Store State
{
student: {
name: "Samadhan",
course: "React JS",
isLoggedIn: true
}
}
In this example, the store contains student information. Different components can read the relevant information without manually passing it through every intermediate component.
3. Important Redux Concepts
1. Store
The store holds the Redux state for the application.
// Example state
{
count: 0
}
2. Action
An action describes what happened or what change is requested. Actions commonly contain a type and may contain additional data.
{
type: "counter/increment"
}
3. Reducer
A reducer is a function that receives the current state and an action, then calculates the next state.
function counterReducer(state = 0, action) {
switch (action.type) {
case "counter/increment":
return state + 1;
default:
return state;
}
}
4. Dispatch
Dispatch sends an action to the Redux store so the reducer can process it and calculate the next state.
dispatch({
type: "counter/increment"
});
4. How Redux Works
Redux follows a predictable, one-way data flow. An action describes a change, the reducer calculates the next state, and the store holds the updated state.
Redux Data Flow
Step-by-Step Explanation
- The user clicks a button in a React component.
- The component dispatches an action.
- The reducer processes the action and calculates the next state.
- The Redux store holds the new state.
- React components subscribed to the changed state can re-render.
5. Simple Counter Example
Consider a counter application. When the user clicks the Increment button, an action is dispatched. The reducer calculates the next counter value.
Counter Reducer
function counterReducer(state = 0, action) {
switch (action.type) {
case "counter/increment":
return state + 1;
case "counter/decrement":
return state - 1;
default:
return state;
}
}
Dispatching Actions
dispatch({
type: "counter/increment"
});
dispatch({
type: "counter/decrement"
});
Expected State Changes
Note: This is a simplified reducer example. A complete Redux application also needs a configured store and the appropriate React-Redux integration.
6. Redux with React
Redux can be integrated with React using the React-Redux library. React-Redux provides tools that allow components to access the Redux store and dispatch actions.
Provider
The Provider makes the Redux store available to the React component tree.
import { Provider } from "react-redux";
import { store } from "./store";
function App() {
return (
<Provider store={store}>
<StudentDashboard />
</Provider>
);
}
useSelector
The useSelector hook reads the required value from the Redux store.
import { useSelector } from "react-redux";
function Counter() {
const count = useSelector(
(state) => state.counter.value
);
return <h2>Count: {count}</h2>;
}
useDispatch
The useDispatch hook provides the dispatch function, which components use to send actions.
import { useDispatch } from "react-redux";
function IncrementButton() {
const dispatch = useDispatch();
return (
<button
onClick={() => dispatch({
type: "counter/increment"
})}
>
Increment
</button>
);
}
state.counter.value path assumes a store structure
that contains a counter slice with a value property. Modern Redux
applications generally use Redux Toolkit to simplify store
configuration and reducer creation.
7. Advantages of Redux
Centralized State
Shared application data can be managed in one store.
Predictable Updates
Actions and reducers provide a structured update process.
Easy Debugging
State changes can be inspected with Redux DevTools.
Component Sharing
Multiple components can access the same application state.
8. Practice Tasks
Task 1: Understand the Store
Create an example state object containing a student's name, course name, and login status.
Task 2: Create Actions
Write action objects for incrementing and decrementing a counter.
Task 3: Write a Reducer
Create a reducer that handles increment, decrement, and reset actions.
Task 4: Explain Redux Flow
Draw a diagram showing how a component dispatches an action, how a reducer calculates the next state, and how the UI receives updated state.
Summary
Redux is a state management library that helps React applications manage shared data through a centralized store. Its core concepts include the store, actions, reducers, and dispatch. Redux follows a predictable one-way data flow in which actions describe changes, reducers calculate the next state, and subscribed components respond to relevant updates. React-Redux connects React components to the store, while Redux Toolkit simplifies modern Redux development. In the next lesson, we will explore Redux Core Concepts in more detail.