redux中间件的简单讲解

redux中间件

中间件的作用: 就是在 源数据目标数据 中间做各种处理,有利于程序的可拓展性,通常情况下,一个中间件就是一个函数,且一个中间件最好只做一件事情

数据源 --------> 中间件 --------> 中间件 --------> 中间件 --------> 目标数据

applyMiddleware

applymiddleware将一堆函数封装成一个函数,这些函数的执行顺序由next传递

柯里化:多参函数->单参函数

复制代码
applyMiddleware(xxxx, xxxx)

手撕thunk

在store 目录下新建 middleware 文件,并创建文件 thunk.js

复制代码
export defualt ({dispatch}) => next => action => {
	if (typeof action === 'function') return action(dispatch)
	return next(action)
}
  1. 当前这个中间件西数不关心你想执行什么样的异步操作 只关心你执行的是不是异步操作

  2. 如果你执行的是异步操作 你在触发action的时候 给我传递一个函数 如果执行的是同步操作 就往下执行

  3. 异步操作代码要写在你传递进来的函数中

  4. 当前这个中间件函数在调用你传递进来的西数时 要将dispatch方法传递过去

在 store 中引入

index.js

复制代码
import { legacy_createStore as createStore, applyMiddleware } from "redux";
import reducer from "./reducers/root.reducer";
import thunk from "./middleware/thunk";

const store = createStore(reducer, applyMiddleware(thunk))

export default store

使用

modal.actions.js

复制代码
import { CHANGEMODALSHOW } from '../const/modal.const'

export const changeModalShow = value => ({type: CHANGEMODALSHOW, value})

export const changeModalShow_async = value => dispatch => {
  setTimeout(()=> {
    dispatch(changeModalShow(value))
  }, 2000)
}

Modal.js

复制代码
import React from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import * as modalActions from '../store/actions/modal.actions'

const Modal = ({isShowModal, changeModalShow, changeModalShow_async}) => {

  const styles = {
    width: '400px',
    height: '400px',
    left: '50%',
    top: '50%',
    position: 'absolute',
    transform: 'translate(-50%, -50%)',
    background: 'aliceblue',
    display: isShowModal ? 'block' : 'none'
  }

  const handelShowModal = () => {
    changeModalShow_async(true)
  }

  const handelHiddenModal = () => {
    changeModalShow(false)
  }

  return (
    <div>
      <button onClick={handelShowModal}>显示</button>
      <button onClick={handelHiddenModal}>隐藏</button>
      <div style={styles}></div>
    </div>
  )
} 

const mapStateToProps = state => ({
  isShowModal: state.modal.isShowModal
})

const mapDispatchToProps = dispatch => bindActionCreators(modalActions, dispatch)

export default connect(mapStateToProps, mapDispatchToProps)(Modal)

redux-thunk

Redux 的Thunk中间件。它允许编写内部带有逻辑的函数,可以与 Redux 存储dispatchgetState方法进行交互

安装

复制代码
npm install redux-thunk

在 store 中引入

复制代码
import { legacy_createStore as createStore, applyMiddleware } from "redux";
import reducer from "./reducers/root.reducer";
import thunk from "redux-thunk";

const store = createStore(reducer, applyMiddleware(thunk))

export default store

效果是跟自己手写thunk效果一样的

相关推荐
AKclown19 分钟前
基于Monaco的diffEditor实现内容对比
前端·vue.js·react.js
chenbin___31 分钟前
react native中 createAsyncThunk 的详细说明,及用法示例(转自通义千问)
javascript·react native·react.js
摆烂工程师32 分钟前
(2025年11月)开发了 ChatGPT 导出聊天记录的插件,ChatGPT Free、Plus、Business、Team 等用户都可用
前端·后端·程序员
gongzemin42 分钟前
使用阿里云ECS部署前端应用
前端·vue.js·后端
用户411800341534143 分钟前
Flutter课题汇报
前端
环信1 小时前
实战教程|快速上线音视频通话:手把手教你实现呼叫与接听全流程
前端
Dgua1 小时前
✨TypeScript快速入门第一篇:从基础到 any、unknown、never 的实战解析
前端
海云前端11 小时前
Vue3 大屏项目投屏功能开发:多显示器适配实践
前端
技术小丁1 小时前
使用 HTML + JavaScript 实现酒店订房日期选择器(附完整源码)
前端·javascript
hashiqimiya1 小时前
harmonyos的鸿蒙的跳转页面的部署
开发语言·前端·javascript