Awesome Reduxcheatsheet
贡献者:BAI
redux-actions
创建一个符合 Flux standard action 格式的 Action
JSincrement = createAction("INCREMENT", (amount) => amount);increment = createAction("INCREMENT"); // 功能同上
JSincrement(42) === { type: "INCREMENT", payload: 42 };
JS// 错误处理err = new Error();increment(err) === { type: "INCREMENT", payload: err, error: true };
flux-standard-action
一种标准的 Flux 格式,Action 可能包含 error、payload、meta 属性
JS{ type: 'ADD_TODO', payload: { text: 'Work it' } }{ type: 'ADD_TODO', payload: new Error(), error: true }
reduce-reducers
组合多个 Reducer 成一个
JSre = reduceReducers( (state, action) => state + action.number, (state, action) => state + action.number);re(10, { number: 2 }); //=> 14
redux-logger
Redux 日志工具
JS// Nothing to see here
Async
redux-promise
Action 接收 Promise 参数
JSincrement = createAction("INCREMENT"); // redux-actionsincrement(Promise.resolve(42));
redux-effects
可以在 Action 上增加一些附加功能,来保证 Action 更干净
JS{ type: 'EFFECT_COMPOSE', payload: { type: 'FETCH' payload: {url: '/some/thing', method: 'GET'} }, meta: { steps: [ [success, failure] ] }}
redux-thunk
Action 接收 Thunks 函数,和 redux-promise 类似
JSfetchData = (url) => (dispatch, getState) => { dispatch({ type: 'FETCH_REQUEST' }) fetch(url) .then((data) => dispatch({ type: 'FETCH_DONE', data }) .catch((error) => dispatch({ type: 'FETCH_ERROR', error })})store.dispatch(fetchData('/posts'))
JS// 也可以简写成fetchData("/posts")(store.dispatch, store.getState);
JS// 因为 fetchData 返回 Promise 所以可以用下面形式来做 SSRstore.dispatch(fetchPosts()).then(() => { ReactDOMServer.renderToString(<MyApp store={store} />);});