0.2.1 • Published 8 years ago
redux-common-reducers v0.2.1
redux-common-reducers :hatching_chick:
Installation
npm install -S redux-common-reducersor
yarn add redux-common-reducersUsage
booleanReducer(trueActions, falseActions, [initialValue = false])
Arguments
trueActions (Array)- The actions' names which sets value totrue.falseActions (Array)- The actions' names which sets value tofalse.[initialValue=false] (boolean)- The initial value of state.
Example
import { booleanReducer } from 'redux-common-reducers';
const myView = combineReducers({
isDoingSth: booleanReducer(
['TRUE_ACTION1', 'TRUE_ACTION2'],
['FALSE_ACTION1', 'FALSE_ACTION2'],
true)
})is same as:
const isDoingSth = (state = true, action) => {
switch (action.type) {
case 'TRUE_ACTION1':
case 'TRUE_ACTION2':
return true;
case 'FALSE_ACTION1':
case 'FALSE_ACTION2':
return false;
default:
return state;
}
}
const myView = combineReducers({
isDoingSth
})numberReducer(changeActions, pathToValue, [initialValue = 0])
Arguments
changeActions (Array)- The actions' names which changes value of state.pathToValue (Array|string)- The path of the property to get. Lodashgetis used under the hood so same rules apply as there. Checklodash.getdocs. :eyes:[initialValue=0] (number)- The initial value of state.
Example
import { numberReducer } from 'redux-common-reducers';
const stats = combineReducers({
errorCount: numberReducer(
['CHANGE_ACTION1', 'CHANGE_ACTION2'],
'path.to.somewhere'
42)
})is same as:
const errorCount = (state = 42, action) => {
switch (action.type) {
case 'CHANGE_ACTION1':
case 'CHANGE_ACTION2':
return action.path.to.somewhere;
default:
return state;
}
}
const stats = combineReducers({
errorCount
})stringReducer(changeActions, resetActions, pathToValue, [initialValue = ''])
Arguments
changeActions (Array)- The actions' names which changes value of state.resetActions (Array)- The actions' names which resets value to default value (initialValue).pathToValue (Array|string)- The path of the property to get. Lodashgetis used under the hood so same rules apply as there. Checklodash.getdocs. :eyes:[initialValue=''] (string)- The initial value of state.
Example
import { stringReducer } from 'redux-common-reducers';
const myView = combineReducers({
name: stringReducer(
['CHANGE_ACTION1', 'CHANGE_ACTION2'],
['RESET_ACTION1'],
'path.to.somewhere')
})The stringReducer will look for value in action.path.to.somewhere.
This is action that will trigger change of name to new value:
{ type: 'CAHNGE_ACTION1', path: { to: { somewhere: 'new value' } } }