redux
设计思想
核心概念
整体流程
store.dispatch(action);如何使用中间件
异步操作
redux-promise
redux-thunk
redux-sega
三种方法差异
react-redux
redux-actions
实现原理
Last updated
store.dispatch(action);Last updated
let nextState = todoApp(previousState, action);// 设置监听函数
store.subscribe(listener); function listerner() {
let newState = store.getState();
component.setState(newState);
}const store = createStore(
reducer,
applyMiddleware(promise,logger) // 中间件引入的先后顺序
);import isPromise from 'is-promise';
import { isFSA } from 'flux-standard-action';
export default function promiseMiddleware({ dispatch }) {
return next => action => {
if (!isFSA(action)) {
return isPromise(action) ? action.then(dispatch) : next(action);
}
return isPromise(action.payload)
? action.payload
.then(result => dispatch({ ...action, payload: result }))
.catch(error => {
dispatch({ ...action, payload: error, error: true });
return Promise.reject(error);
})
: next(action);
};
}function createThunkMiddleware(extraArgument)
{
return ({ dispatch, getState }) => next => action => {
if (typeof action === 'function') {
return action(dispatch, getState, extraArgument);
}
return next(action);
};
}
const thunk = createThunkMiddleware();
thunk.withExtraArgument = createThunkMiddleware;
export default thunk;