Introduction to Redux
Redux is a state management library that works well with JavaScript applications, including React. It provides a predictable state container and helps manage the state of an application in a centralized manner.
The core concept of Redux involves three main components: the store, actions, and reducers.
Store:
The store is a single source of truth for the state of your application. It holds the state data and provides methods to update and access the state.
Actions:
Actions are plain JavaScript objects that describe an event in your application. They are dispatched to the store and are the only way to update the state.
Reducers:
Reducers are pure functions that define how the application's state should change in response to an action. They take the current state and an action as parameters and return the next state.
Here's a simple example of how Redux works:
1// The initial state
2const initialState = {
3 count: 0,
4};
5
6// The reducer function
7function reducer(state = initialState, action) {
8 switch (action.type) {
9 case 'INCREMENT':
10 return {
11 ...state,
12 count: state.count + 1,
13 };
14 case 'DECREMENT':
15 return {
16 ...state,
17 count: state.count - 1,
18 };
19 default:
20 return state;
21 }
22}
23
24// Create a Redux store
25const store = Redux.createStore(reducer);
26
27// Dispatch actions to update the state
28store.dispatch({ type: 'INCREMENT' });
29store.dispatch({ type: 'INCREMENT' });
30store.dispatch({ type: 'DECREMENT' });
31
32// Get the current state
33const currentState = store.getState();
34console.log(currentState);
This example demonstrates the basic usage of Redux. We start with an initial state object that contains a count
property set to 0. We define a reducer function that handles two types of actions: INCREMENT
and DECREMENT
. When an INCREMENT
action is dispatched, the reducer increments the count by 1. When a DECREMENT
action is dispatched, the reducer decrements the count by 1. Finally, we create a Redux store using the reducer function, dispatch actions to update the state, and get the current state using store.getState()
.
Understanding the core concepts of Redux is essential for building scalable and maintainable frontend applications.
xxxxxxxxxx
console.log(currentState);
// Introduction to Redux
// Redux is a state management library that works well with JavaScript applications, including React. It provides a predictable state container and helps manage the state of an application in a centralized manner.
// The core concept of Redux involves three main components: the store, actions, and reducers.
// 1. Store: The store is a single source of truth for the state of your application. It holds the state data and provides methods to update and access the state.
// 2. Actions: Actions are plain JavaScript objects that describe an event in your application. They are dispatched to the store and are the only way to update the state.
// 3. Reducers: Reducers are pure functions that define how the application's state should change in response to an action. They take the current state and an action as parameters and return the next state.
// Here's a simple example of how Redux works:
// The initial state
const initialState = {
count: 0,
};
// The reducer function
function reducer(state = initialState, action) {
switch (action.type) {
case 'INCREMENT':
return {
state,
count: state.count + 1,
};
case 'DECREMENT':
return {