• 解决reudx中的异步问题 applyMiddleware thunk


     applyMiddleware

    redux中的异步解决方案

     component中通过dispatch传递action到store中,store通过middleware处理一些功能(异步功能),传递action到reducer,又会返回新的state到store中,store中的state发生改变会触发subscribe来对component重新渲染

    redux整个流程本身事同步的

    middleware API 来自于redux。redux/applyMiddleware.md at master · reduxjs/redux · GitHub

    • 引入方式 import {createStore,applyMiddleware} from 'redux'
    • 执行位置 let store = createStore( reducer ,applyMiddleware(test,logger))
    • 参数是自定义中间件
      • 中间件函数的写法 是 固定不变的
      • 每个中间件函数内部必须调用next()
      • 中间件函数执行的顺序跟在applyMiddleware() 中的参数有关
        let store = createStore( reducer ,applyMiddleware(test,logger))
      • 虽然中间件可以拓展redux中的应用程序,可以添加异步代码,但是会造成所有的action都会出现异步功能,这不是想要的结果,中间件只是告诉我们, 谁(action)是异步的,谁不是异步的

     

    中间件src/store/middleware/logger.js

    1. // function logger({ getState }) {
    2. // return next => action => {
    3. // const returnValue = next(action)
    4. // return returnValue
    5. // }
    6. // }
    7. export function logger(store){
    8. return function(next){
    9. return function(action){
    10. console.log('1111')
    11. next(action)
    12. }
    13. }
    14. }
    15. // 添加异步代码
    16. export function logger(store){
    17. return function(next){
    18. return function(action){
    19. let t = setTimeout(() => {
    20. console.log('1111')
    21. },2000)
    22. next(action)
    23. }
    24. }
    25. }

    中间件 src/store/middleware/test.js

    1. export function test(store){
    2. return function(next){
    3. return function(action){
    4. console.log('2222')
    5. next(action)
    6. }
    7. }
    8. }

    store/index.js

    1. import {createStore,applyMiddleware} from 'redux'
    2. import reducer from './reducer'
    3. import {logger} from './middleware/logger'
    4. import {test} from './middleware/test'
    5. //1. 定义store
    6. let store = createStore( reducer ,applyMiddleware(test,logger))
    7. export default store

    redux-thunk. 中间件解决异步的方法 

    将异步代码写在 action中,中间件thunk只是告诉我们,谁(action)是异步谁不是

    action 返回的是一个函数而不是一个对象

    action.js

    1. // 4. 定义action
    2. // let increment = {type:'increment'}
    3. // let decrement = {type:'decrement'}
    4. // export function increment(){
    5. // return{
    6. // type:'increment'
    7. // }
    8. // }
    9. // export function decrement(){
    10. // return{
    11. // type:'decrement'
    12. // }
    13. // }
    14. export const increment = (num) => ({ type:'increment',payload:num }) // 同步action
    15. // export const incrementasync = function(num){
    16. // return function(dispatch){
    17. // let t = setTimeout(() => {
    18. // dispatch(increment(num))
    19. // },2000)
    20. // }
    21. // }
    22. export const incrementasync = (num) => (dispatch) => { //异步action
    23. let t = setTimeout(() => {
    24. dispatch(increment(num))
    25. },2000)
    26. }
    27. export const decrement = (num) => ({ type:'decrement',payload:num })
    28. export const fngreen = () => ({ type:'fngreen'})
    29. export const fnred = () => ({ type:'fnred' })

    中间件thunk.js

    1. export function thunk(store){
    2. return function(next){
    3. return function(action){
    4. if(typeof action === 'function'){
    5. action(store.dispatch)
    6. }else{
    7. next(action)
    8. }
    9. }
    10. }
    11. }

  • 相关阅读:
    git常用命令
    LayaBox---TypeScript---类型兼容性
    【python 】----Pytest基础知识与进阶知识
    【Flask基础】一,app对象的初始化与静态参数配置
    C++初级---模板初阶
    2023高教社杯数学建模国赛C题思路解析+代码+论文
    websocket技术详解,附带springboot实例
    Linux安装frp并实现内网穿透
    6-Dubbo架构设计与底层原理-服务导出源码分析(下)
    一文搞清楚前端 polyfill
  • 原文地址:https://blog.csdn.net/weixin_41040445/article/details/126049906