@christianalfoni
A data you use to populate a template
STATE
dispatch
{
type: "EXAMPLE_ACTION",
payload: {}
}
const initialState = {}
function reducer(state = initialState, action) {
return state
}
dispatch
The output of a pure functions depends only on inputs
Has no side-effects
Pure functions have no state
Reducers are pure functions
ActionA
ActionC
ActionB
Kept in Store
- Hold the state
- Expose state by store.getState()
- Update state by store.dispatch(action)
- Add a listener with store.subscribe(listener)
// Create initial state
const initialState = {
value: null,
};
const store = createStore(reducer, initialState);
// Get current snapshot
store.getState()
// Initiate action
store.dispatch({type: 'SOME_ACTION', value: 'EX_VALUE'});
// Called once there is change in state
store.subscribe(() => {
// Do something
});
// Reducer
testReducer(state, action) => {
switch (action.type) {
case SOME_ACTION:
return Object.assign({}, state, {
value: action.value,
});
default:
return state;
}
}
Dan Abromov
http://redux.js.org/docs/faq/General.html
Brevity
Predictability
Atsushi Yamamoto
@jumbosushi