美文网首页
Redux Thunk中间件

Redux Thunk中间件

作者: sdcV | 来源:发表于2017-07-12 18:39 被阅读121次

Action创建函数就是创建action对象的函数,起初它只能返回action对象,但通过中间件的加工后,action创建函数可以返回更多的类型。Redux Thunk中间件就是去加工action创建函数。

  1. Action创建函数
    function increment(){
    return {type: 'INCREMENT'};
    }
    发起:store.dispatch(increment());
  2. Redux Thunk中间件(需安装react-thunk包)
    function incrementIfOdd(){
    return (dispatch, getState){
    const value = getState();
    if(value % 2 ===0){
    return;
    }
    dispatch(increment());
    };
    }
    激活:const store = createStore(counter, applyMiddleware(thunk));
    store.dispatch(incrementIfOdd());

相关文章

网友评论

      本文标题:Redux Thunk中间件

      本文链接:https://www.haomeiwen.com/subject/plzbhxtx.html