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 详解
网友评论