react-Middleware

2017-07-25  本文已影响30人  07120665a058

react单向数据流

Middleware

import request from 'superagent';    
export default store=>next=>action=> {
    switch (action.type) {
        case 'ADD_TASK':
            request.post('/add')
                .send({"username":action.username})
                .end((err, res)=> {
                    next({type: 'SHOW_TASK'})
                })
    }
    next(action);
}
import thunkMiddleware from 'redux-thunk';  //异步action
import {createLogger} from 'redux-logger';   //可以输出state的变化log,nextState
import reducer from './reducers';
const Middleware = applyMiddleware(thunkMiddleware, createLogger());
let store = createStore(reducer, Middleware);

如果在middleware中调用的是store.dispatch(action)而不是next(action),那么会再次遍历包含当前middleware在内的整个 middleware

参考文章推荐:
Redux 中间件 Middleware 详解

上一篇 下一篇

猜你喜欢

热点阅读