npm install -S redux-common-reducers
or
yarn add redux-common-reducers
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
})
Arguments
changeActions (Array)
- The actions' names which changes value of state.pathToValue (Array|string)
- The path of the property to get. Lodashget
is used under the hood so same rules apply as there. Checklodash.get
docs. 👀[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
})
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. Lodashget
is used under the hood so same rules apply as there. Checklodash.get
docs. 👀[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' } } }