Understanding the Redux store

What is Redux? Redux is an open-source JavaScript library that helps manage the state of an application. It is commonly used with React, but can also be used with other JavaScript frameworks or even vanilla JavaScript. Redux follows the principles of the Flux architecture, providing a predictable state container for your application.

The Redux Store At the heart of Redux is the store, which holds the state of your application. The Redux store is a JavaScript object that contains all the data that your application needs. It is the single source of truth, meaning that all components access and modify the state through the store.

Store Setup Setting up a Redux store involves defining a reducer function. A reducer is a pure function that receives the current state and an action and returns a new state based on the action type. This function is responsible for handling the logic of your application’s state changes.

import { createStore } from 'redux';

// Reducer function
function reducer(state = initialState, action) {
  switch (action.type) {
    case 'INCREMENT':
      return {
        ...state,
        count: state.count + 1
      };
    case 'DECREMENT':
      return {
        ...state,
        count: state.count - 1
      };
    default:
      return state;
  }
}

// Create Redux store
const store = createStore(reducer);

Actions and Dispatching To update the state in the Redux store, you need to dispatch actions. An action is a plain JavaScript object that describes the type of change you want to make in the state. It can also carry additional data that is necessary for the state update.

// Dispatching an action
store.dispatch({ type: 'INCREMENT' });

// Accessing the state
console.log(store.getState()); // { count: 1 }

Subscribing to Store Changes To react to changes in the Redux store, you can subscribe to the store. This allows your components to get notified whenever the state changes.

// Subscribe to store changes
store.subscribe(() => {
  console.log(store.getState()); // { count: 2 }
});

// Dispatch another action
store.dispatch({ type: 'INCREMENT' });

Conclusion Understanding the Redux store is crucial when working with Redux. It serves as a centralized place for storing and managing the state of your application. By dispatching actions and subscribing to store changes, you can efficiently update and access the state across your application.

#Redux #StateManagement