美文网首页
react-Middleware

react-Middleware

作者: 07120665a058 | 来源:发表于2017-07-25 15:43 被阅读30次

react单向数据流

  • 点击button后,在回调中dispatch一个action,reducer收到 action后,更新state并通知view重新渲染

Middleware

  • 为了增强dispatch而出现
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 详解

相关文章

  • react-Middleware

    react单向数据流 点击button后,在回调中dispatch一个action,reducer收到 actio...

网友评论

      本文标题:react-Middleware

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