React Redux Tutorial

In this tutorial, you will learn how to use Redux library in your react application, So, let's get started.

What is Redux?

  1. Redux is a predictable state container for managing the state of a JavaScript application.
  2. It's often used with React but can be integrated into any JavaScript framework or library.

How does Redux work?

  1. Redux works by storing application state in a single store.
  2. The store is an object that holds all of the data that the application needs to run.
  3. The only way to change the state in the store is to dispatch an action.
  4. An action is an object that describes what happened in the application.
  5. For example, an action might be dispatched when the user clicks a button or when data is fetched from an API.
  6. Reducers are functions that handle actions and update the state in the store.
  7. When an action is dispatched, all of the reducers in the application are called.
  8. Each reducer can update the state in the store however it needs to.

Getting started with Redux

  1. To get started with Redux, you will need to install the Redux and React Redux packages.
  2. npm install redux react-redux
  3. Once you have installed the Redux and React Redux packages, you can create a Redux store. You can do this by creating a store.js file and adding the following code.
import { createStore } from 'redux';

const reducer = () => {
  return {};
};

const store = createStore(reducer);

export default store;
  

Redux Actions

  1. Actions are plain JavaScript objects describing what happened.
  2. They are dispatched to the store using the dispatch method.
const increment = () => {
  return {
    type: 'INCREMENT',
  };
};

store.dispatch(increment()); // Dispatch the action

Redux Reducers

  1. Reducers specify how the application's state changes in response to actions.
  2. They are pure functions that take the previous state and an action and return a new state.
const initialState = 0; // Initial state
const reducer = (state = initialState, action) => {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1;
    default:
      return state;
  }
};
  

Connecting Redux to React

  1. To connect Redux to a React application, use the Provider component from react-redux to wrap your app.
import React from 'react';
import { Provider } from 'react-redux';
import store from './store'; // Your Redux store

function App() {
  return (
    <Provider store={store}>
      {/* Your app components */}
    </Provider>
  );
}
  

Using Redux in React Components

  1. Connect Redux store to your React components using the connect function from react-redux.
import { connect } from 'react-redux';

  function Counter({ count, increment }) {
    return (
      <div>
        <p>Count: {count}</p>
        <button onClick={increment}>Increment</button>
      </div>
    );
  }
  
  const mapStateToProps = (state) => {
    return {
      count: state,
    };
  };
  
  const mapDispatchToProps = (dispatch) => {
    return {
      increment: () => dispatch(increment()),
    };
  };
  
export default connect(mapStateToProps, mapDispatchToProps)(Counter);
    

Redux Middleware

  1. Middleware in Redux allows you to add extra functionality to the dispatch process.
  2. Popular middleware includes redux-thunk for handling async actions and redux-logger for logging.
import { applyMiddleware, createStore } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';

const store = createStore(rootReducer, applyMiddleware(thunk));