-
Notifications
You must be signed in to change notification settings - Fork 292
Reducers
Diego Haz edited this page May 19, 2017
·
6 revisions
Reducers are pure functions which are called sequentially when some action
is dispatch
ed. With them you can perform state changes based on the dispatched action.
A simple reducer will look like this:
const reducer = (state, action) => {
switch (action.type) {
case 'RESOURCE_CREATE':
return {
...state,
list: [action.data, ...state.list],
}
default:
return state
}
}
As well as actions, reducers are pure functions and therefore very easy to unit test:
test('RESOURCE_CREATE', () => {
const state = {}
const action = {
type: 'RESOURCE_CREATE',
data: { title: 'Hi!' },
}
const expectedState = {
list: [{ title: 'Hi!' }],
}
expect(reducer(state, action)).toEqual(expectedState)
})
Special thanks to @kybarg and @protoEvangelion for helping to write this Wiki. Please, feel free to edit/create pages if you think it might be useful (also, see #33)