zoukankan      html  css  js  c++  java
  • Redux 中间件的执行顺序理解

    Redux.applyMiddleware(thunk, middleware1) 和 Redux.applyMiddleware(middleware1, thunk) 的区别:

    <!DOCTYPE HTML>
    <html>
        <head>
            <meta http-equiv="content-type" content="text/html; charset=utf-8">
            <title>Redux</title>
            <script type="text/javascript" src='js/redux.js'></script>
        </head>
        <body>
            <script type="text/javascript">
                function reducer(state, action) {
                  // 首次调用本函数时设置初始 state
                  state = state || { counter: 0 };
    
                  switch (action.type) {
                    case 'INCREMENT':
                      console.log('reducer');
                      return { counter: state.counter + 1 };
                    case 'DECREMENT':
                      return { counter: state.counter - 1 };
                    default:
                      return state; // 无论如何都返回一个 state
                  }
                }
    
                var thunk = (middleApi) => (next) => (action) => {
                  if(typeof action == 'function'){
                    console.log(1);
                    return action(middleApi.dispatch, middleApi.getState);
                  }
    
                  console.log(2);
                  return next(action);
                }
    
                function middleware1(store) {
                  return function(next) {
                    return function(action) {
                      console.log('middleware1 开始');
                      next(action);
                      console.log('middleware1 结束');
                    };
                  };
                }
    
                var inc = () => {
                  return {type: 'INCREMENT'}
                }
    
                var incAsy = () => (dispatch) => {
                  console.log('等待2秒');
                  setTimeout( ()=>{
                    dispatch( inc() );
                  }, 2000);
                }
    
                function incAsy2(){
                  return (dispatch, getState)=>{
                    console.log('等待1秒');
                    setTimeout( ()=>{
                      dispatch( incAsy() );
                      console.log('incAsy2 ');
                    } ,1000);
                  }
                }
    
                /*
                  Redux.applyMiddleware(thunk, middleware1)
                  log 如下:
                    1
                    等待1秒
                    1
                    等待2秒
                    incAsy2
                    2
                    middleware1 开始
                    reducer
                    middleware2 开始
                */
    
                /*
                  Redux.applyMiddleware(middleware1, thunk)
                  log 如下:
                    middleware1 开始
                    1
                    等待1秒
                    middleware1 结束
    
                    //注意这里触发 dispatch, 又从 middleware1 里面进去了
                    middleware1 开始
                    1
                    等待2秒
                    middleware1 结束
                    incAsy2
    
                    middleware1 开始
                    2
                    reducer
                    middleware1 结束
                */
    
                var store = Redux.applyMiddleware(thunk, middleware1)(Redux.createStore)(reducer);
                store.dispatch( incAsy2() );
    
            </script>
        </body>
    </html>
  • 相关阅读:
    Java的参数传递是「按值传递」还是「按引用传递」?
    算法08 五大查找之:二叉排序树(BSTree)
    算法07 五大查找之:索引查找
    算法06 五大查找之:二分查找
    Django rest_framework实现RESTful API
    jenkins + pipeline构建自动化部署
    jenkins部署.net平台自动化构建
    python实现编写windows服务
    iis部署python运行环境
    Jenkins执行批处理文件、powershell失败
  • 原文地址:https://www.cnblogs.com/zhengming2016/p/6985354.html
Copyright © 2011-2022 走看看