Skip to content

Instantly share code, notes, and snippets.

@mattiamanzati
Created October 5, 2016 12:25
Show Gist options
  • Save mattiamanzati/fdf987121f3f125ad28561b7d8961437 to your computer and use it in GitHub Desktop.
Save mattiamanzati/fdf987121f3f125ad28561b7d8961437 to your computer and use it in GitHub Desktop.
// ...
// initial state
const initialState = {
left: CounterPair.reducer(),
right: CounterPair.reducer(),
count: 0
}
// listen to any action of subcomponents
const appReducer = createReducer(
initialState,
{
[Counter.INCREMENT]: state => updateProperty(state, 'count', state.count + 1)
}
)
// actual reducer
export const reducer = (state = initialState, action) => {
// child reducers
state = updateProperty(state, 'left', CounterPair.reducer(state.left, actionFor(LEFT, action)))
state = updateProperty(state, 'right', CounterPair.reducer(state.right, actionFor(RIGHT, action)))
// listen to any children event
return appReducer(state, unwrap(action))
}
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment