@beardedtim/common-reducers
v0.1.0
Published
> Because I got tired of re-writing these every reducer
Downloads
1
Readme
Common Reducers
Because I got tired of re-writing these every reducer
Array Methods
Usage
import {add, updateByKey, removeByKey} from '@beardedtim/common-reducers'
const reducer = {
[ADD_USER]: add,
[UPDATE_USER]: updateByKey('_id'),
[REMOVE_USER]: removeByKey('_id')
}
which is dependent on the action looking like this:
addUser(user) // {type: ADD_USER, payload: user}
updateUser({_id: _id, update}) // {type: UPDATE_USER, payload: {_id,update}}
removeUser(_id) // {type: REMOVE_USER, payload: _id}
If you want to remove more than one item at a time, you can use unique=false
when creating removeByKey
:
[REMOVE_MATCHING_NAME]: removeByKey('name',false)
this will now filter out all objects inside of state
who have a property called name
that is set to whatever is passed in by removeUser