• redux中间件的简单讲解


    redux中间件

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

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

    applyMiddleware

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

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

    applyMiddleware(xxxx, xxxx)
    
    • 1

    手撕thunk

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

    export defualt ({dispatch}) => next => action => {
    	if (typeof action === 'function') return action(dispatch)
    	return next(action)
    }
    
    • 1
    • 2
    • 3
    • 4
    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    使用

    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)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    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 (
        
    ) } const mapStateToProps = state => ({ isShowModal: state.modal.isShowModal }) const mapDispatchToProps = dispatch => bindActionCreators(modalActions, dispatch) export default connect(mapStateToProps, mapDispatchToProps)(Modal)
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    redux-thunk

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

    安装
    npm install redux-thunk
    
    • 1
    在 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

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

  • 相关阅读:
    爬虫学习 | 02 认识爬虫spider
    NR 物理层编码 - slide4 循环码Cyclic Code
    vue课程80 演示如何利用cli创建项目
    十、MFC控件(二)
    Postgresql on conflict do update 设置当前值,原始值,当前值与原始值相加值
    python操作kafka
    SpringBoot2.0---------------14、SpringBoot中Web原生组件注入
    【数据结构】树与二叉树(七):二叉树的遍历
    Cilium 系列-2-Cilium 快速安装
    Lucene全文检索
  • 原文地址:https://blog.csdn.net/weixin_44872023/article/details/132783283