Home Web Front-end JS Tutorial Detailed explanation of redux-thunk practical project cases

Detailed explanation of redux-thunk practical project cases

Jun 07, 2018 pm 02:55 PM
redux middleware

This time I will bring you a detailed explanation of the redux-thunk practical project case. What are the precautions for using redux-thunk practical project? The following is a practical case, let’s take a look.

The core concept of redux is actually very simple: store all the state that needs to be modified into the store, initiate an action to describe what happened, and use reducers to describe how the action changes the state tree. When creating a store, you need to pass in the reducer. What can really change the data in the store is the store.dispatch API.

1. Concept

After dispatching an action and before reaching the reducer, you need to use middleware to perform some additional operations. You can use Redux middleware for logging, creating crash reports, calling asynchronous interfaces or routing, and more.
In other words, middleware is an enhancement to store.dispatch()

2. Usage of middleware

import { applyMiddleware, createStore } from 'redux';
import thunk from 'redux-thunk';
const store = createStore(
  reducers, 
  applyMiddleware(thunk)
);
Copy after login

Introduce thunk middleware directly and put In the applyMiddleware method, passing in the createStore method completes the functional enhancement of store.dispatch(). That is, you can perform some asynchronous operations in the reducer.

3.applyMiddleware()

In fact, applyMiddleware is a native method of Redux, which combines all middleware into an array and executes them in sequence.

If there are too many middlewares, they can be passed in as parameters one after another

const store = createStore(
  reducers, 
  applyMiddleware(thunk, logger)
);
Copy after login

If you want to understand its evolution, you can go to the official documentation of redux: https://redux.js.org/advanced/middleware

4.redux-thunk

Analyze the source code of redux-thunk node_modules/redux-thunk/src/index.js

function createThunkMiddleware(extraArgument) {
 return ({ dispatch, getState }) => next => action => {
  if (typeof action === 'function') {
   return action(dispatch, getState, extraArgument);
  }
  return next(action);
 };
}
const thunk = createThunkMiddleware();
thunk.withExtraArgument = createThunkMiddleware;
export default thunk;
Copy after login

redux-thunk The middleware export default is the thunk passed by createThunkMiddleware(). Looking at the createThunkMiddleware function, it returns a curried function. The code we translate into ES5 is easier to read.

function createThunkMiddleware(extraArgument) {
  return function({ dispatch, getState }) {
    return function(next){
      return function(action){
        if (typeof action === 'function') {
          return action(dispatch, getState, extraArgument);
        }
        return next(action);
      };
    }
  }
}
Copy after login

With this look, we can see that the most important idea of ​​redux-thunk is that it can accept an action creator that returns a function. If the action creator returns a function, execute it. If not, execute it according to the original next(action).

Because this action creator can return a function, you can perform some asynchronous operations in this function.

For example:

export function addCount() {
  return {type: ADD_COUNT}
}
export function addCountAsync() {
  return dispatch => {
    setTimeout( () => {
      dispatch(addCount())
    },2000)
  }
}
Copy after login

The addCountAsync function returns a function, pass dispatch as the first parameter of the function, and perform asynchronous operations within the function.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use JS to get the last 7 days and the last 3 days

Angular ng-animate and How to use ng-cookies within the project

The above is the detailed content of Detailed explanation of redux-thunk practical project cases. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement custom middleware in CodeIgniter How to implement custom middleware in CodeIgniter Jul 29, 2023 am 10:53 AM

How to implement custom middleware in CodeIgniter

What is the principle of tomcat middleware What is the principle of tomcat middleware Dec 27, 2023 pm 04:40 PM

What is the principle of tomcat middleware

How to use middleware for data acceleration in Laravel How to use middleware for data acceleration in Laravel Nov 02, 2023 am 09:40 AM

How to use middleware for data acceleration in Laravel

How to use middleware for response transformation in Laravel How to use middleware for response transformation in Laravel Nov 03, 2023 am 09:57 AM

How to use middleware for response transformation in Laravel

Analysis of the relationship between PHP real-time communication function and message push middleware Analysis of the relationship between PHP real-time communication function and message push middleware Aug 10, 2023 pm 12:42 PM

Analysis of the relationship between PHP real-time communication function and message push middleware

How to use middleware for scheduled task scheduling in Laravel How to use middleware for scheduled task scheduling in Laravel Nov 02, 2023 pm 02:26 PM

How to use middleware for scheduled task scheduling in Laravel

How to handle form validation using middleware in Laravel How to handle form validation using middleware in Laravel Nov 02, 2023 pm 03:57 PM

How to handle form validation using middleware in Laravel

CakePHP middleware: Integrate push notifications and message reminders to achieve real-time notifications CakePHP middleware: Integrate push notifications and message reminders to achieve real-time notifications Jul 29, 2023 pm 04:33 PM

CakePHP middleware: Integrate push notifications and message reminders to achieve real-time notifications

See all articles