ReduxReducer

A Reducer is a pure function that takes the state of an application and action as arguments and returns a new state.

1
2
3
4
5
6
7
8
9
10
11
const initialState = {};
const action = {
type: 'ADD_TO_CART',
payload: {
product: 'margarine',
quantity: 4
}
}
const cartReducer = (state = initialState, action) => {
// Do something here
}

combineReducers的简单实现

1
2
3
4
5
6
7
8
const combineReducers = reducers => {
return (state = {}, action) => {
return Object.keys(reducers).reduce((nextState, key) => {
nextState[key] = reducers[key](state[key], action);
return nextState;
},{});
};
};