Implementing Redux in React

Implementing Redux in React

Learn how to implement Redux in React. Redux is a state management library, Redux establishes the idea of a centralized global state and prevents components from directly accessing and altering the information stored in the global state.

Redux is a state management library, it establishes the idea of a centralized global state and prevents components from directly accessing and altering the information stored in the global state. By doing so, it establishes a uniform flow of information, unlike React where information flows up and down a component tree. Components are provided with the ability to trigger an update event by dispatching an action that informs a receiver function known as a reducer of the specific way the state should be updated. The reducer uses the information stored in the action, updates the state and the component is then, and automatically, provided with the updated information via its subscription to the global state. After understanding this basic flow, you can begin implementing Redux. To start off, you will have a few libraries:

npm i --save redux react-redux

Here we are first installing the Redux library and the second library “react-redux” connects Redux and the global state to the React application. After this is done, we can begin configuring the React application.

// index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import App from './App';
import reducer from './store/reducer';
const store = createStore(reducer);
const app = (
  <Provider store={store}>
    <App />
  </Provider>
);
ReactDOM.render(app, document.getElementById('root'));

Let’s breakdown the first step. Redux is configured within the same file that React is set up, in the index.js file. First, the “createStore” function is imported which, as the name implies, helps to create the central store or the global state for Redux. Next, the “Provider” helper component is imported which helps to connect the React application to Redux, giving it access to the global state. Afterward, an import of a placeholder “reducer” is made for the sake of setting up the initial configuration within the index.js file. The import accesses a reducer file from a directory named “store.” This is the convention to create a directory named store that will contain all Redux code. Next, the global state is created using the createStore function, and it takes in as an argument the reducer, not yet created, that was imported, which will connect the reducer to the created global state. Finally, the Provider helper component wraps the App component allowing the React application a connection to Redux and its global state. Also, note that the Provider component takes in a mandatory prop called store and is passed the store that was created with the createStore function, and the reducer passed as an argument. With this, the React application is now connected to Redux.

After configuring Redux, we move to actually creating the reducer.js file within a directory named “store” which will contain all Redux-related code.

// within src/store/reducer.js

const initialState = {
  someProp: someValue,
  anotherProp: anotherValue
};
const reducer = (state=initialState, action) => {
  switch(action.type) {
    case 'SOME_ACTION':
      return {
        ...state,
        someProp: someValueUpdated
      };
    case 'ANOTHER_ACTION':
      return {
        ...state,
        anotherProp: anotherValue + action.actionPayload
      };
    default:
      return state;
  }
};
export default reducer;

Alright to start off, a reducer acts on the global state, updating it based on the information it receives from an action. And in order for a reducer to begin updating the global state, it needs to be defined. To do this, an initial state can be created with some starting properties. Next, the reducer function is defined, and it takes two arguments, the initial state and the action that will inform it on how to update the global state. By convention, the reducer is written with a switch statement and will return a new state with some update based on the type of change stored in the action. Also to note, the action types or the type property on the action object stores an all upper case string, again this is the convention. In the case of ‘ANOTHER_ACTION’, the state is updated using a payload which is simply another property, besides the type property, on the action object. That property holds some value that is used in some way to update the global state. With this, the reducer is set up and exported and has been configured within the index.js file. Now all that is left is to connect, or subscribe, the React component to the Redux state.

web-development javascript redux react developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

Learn React Redux With React Hooks - React JavaScript Tutorial

In this video I wanted to show the basics of Redux in React, I didn't go over anything complex as I wanted this to be a beginners introduction into Redux. Let me know in the comments if you would like to see more content on redux such as Redux Thunk.

Hire Dedicated React Native Developer

Have you ever thought of having your own app that runs smoothly over multiple platforms? React Native is an open-source cross-platform mobile application framework which is a great option to create mobile apps for both Android and iOS. **[Hire...

Hire Dedicated React Native Developer in India | React Native Development

Hire dedicated React Native developers for your next project. As the top react native development company we offer you the best service as per your business needs.

React With Redux, React Router And API Integration - React.js Tutorial

In this video I wanted to show you guys how to put Redux, and React router together along side a simple api to show some data. Let me know if you guys enjoy this sort of video, where I cover more than just a single package.