Out of the box reducer creators.
yarn add easydux
import { object } from 'easydux';
const myReducer = object({
SET: 'SET_ACTION_TYPE',
CLEAR: 'CLEAR_ACTION_TYPE',
});
const store = createStore(myReducer);
store.dispatch({
type: 'SET_ACTION_TYPE',
data: { hello: 'world' },
});
store.getState();
// { "hello": "world" }
store.dispatch({
type: 'CLEAR_ACTION_TYPE',
});
store.getState();
// { }
These are just reducers. You could even call them like this:
const myObjectReducer = object({
SET: 'SET_ACTION_TYPE',
CLEAR: 'CLEAR_ACTION_TYPE',
});
function myReducer(state, action) {
switch (action.type) {
case 'MY_CUSTOM_ACTION':
return { my: 'custom behavior' };
default:
return myObjectReducer(state, action);
}
}
You'll likely just include them with combineReducers
like this though.
const myObjectReducer = object({
SET: 'SET_ACTION_TYPE',
CLEAR: 'CLEAR_ACTION_TYPE',
});
const reducer = combineReducers({
reducerKey: myObjectReducer,
});
createStore(reducer);
Returns a reducer that sets or clears a single value.
actionTypes
: object no single type is requiredSET
: string the action type to set the stateCLEAR
: string the action type to clear the state
default
: any default state - will be reset to this state onCLEAR
defaults to null
import { value } from 'easydux';
const myValueReducer = value({
SET: 'SET_VALUE_TYPE',
CLEAR: 'CLEAR_VALUE_TYPE',
});
- type:
SET
Sets reducer state.- data:
state
|function<newState>(state, action)
- data:
- type:
CLEAR
Sets reducer to default.
const mySetValueActionCreator = data => ({
type: 'SET_VALUE_TYPE',
data: 'new data',
});
// or
const mySetValueActionCreator = data => ({
type: 'SET_VALUE_TYPE',
data: state => 'new data',
});
const myClearValueActionCreator = data => ({
type: 'CLEAR_VALUE_TYPE',
});
Returns a reducer that sets or clears a single value.
actionTypes
: object no single type is requiredUPDATE
: string uses package immutability-helper to set new stateSET
: string the action type to set the stateCLEAR
: string the action type to clear the stateSET_AT
: string the action type to set a single item in the stateINSERT_AT
: string the action type to insert a single item in the statePUSH
: string the action type to push a single element onto the state at the endUNSHIFT
: string the action type to push some elements onto the state at the frontCONCAT
: string the action type to push some elements onto the state at the endCONCAT_TO
: string the action type to push some elements onto the state at the frontPOP
: string the action type to remove a single element from the end of the arraySHIFT
: string the action type to remove a single element from the front of the arrayREMOVE_AT
: string the action type to remove a single item from the stateSLICE
: string the action type to slice the state into a subsetMAP
: string the action type to map the stateFILTER
: string the action type to filter the state
default
: array default state - will be reset to this state onCLEAR
defaults to []
import { array } from 'easydux';
const myArrayReducer = array({
UPDATE: 'UPDATE_ARRAY',
SET: 'SET_ARRAY',
CLEAR: 'CLEAR_ARRAY',
SET_AT: 'SET_AT_ARRAY',
INSERT_AT: 'INSERT_AT_ARRAY',
PUSH: 'PUSH_ARRAY',
UNSHIFT: 'UNSHIFT_ARRAY',
CONCAT: 'CONCAT_ARRAY',
CONCAT_TO: 'CONCAT_TO_ARRAY',
POP: 'POP_ARRAY',
SHIFT: 'SHIFT_ARRAY',
REMOVE_AT: 'REMOVE_AT_ARRAY',
SLICE: 'SLICE_ARRAY',
MAP: 'MAP_ARRAY',
FILTER: 'FILTER_ARRAY',
});
import { array } from 'easydux';
import { createStore } from 'redux';
const reducer = array({
SET: 'MY_ARRAY_SET',
});
const store = createStore(reducer);
store.dispatch({
type: 'MY_ARRAY_SET',
data: ['new', 'state', 'new', null],
uniq: true, // optional
compact: true, // optional
sort: item => -item.length,
});
store.getState();
// [ 'state', 'new' ]
- type:
UPDATE
Updates reducer state. See https://github.com/kolodny/immutability-helper- data:
config
|object
- data:
- type:
SET
Sets reducer state.- data:
state
|function<newState>(state, action)
- ...post-processors: see below
- data:
- type:
CLEAR
Sets reducer to default. - type:
SET_AT
Sets value at index.- index:
number
- data:
state
|function<newState>(state[index], action, state)
- ...post-processors: see below
- index:
- type:
INSERT_AT
Inserts value at index, adding one element to the state.- index:
number
: index of new value - data:
state
|function<newState>(null, action, state)
- ...post-processors: see below
- index:
- type:
PUSH
Adds value to end of state.- data:
state
|function<newState>(null, action, state)
- ...post-processors: see below
- data:
- type:
UNSHIFT
Adds value to front of state.- data: value | (null, action, state) => value:
state
orfunction<newState>(null, action, state)
- ...post-processors: see below
- data: value | (null, action, state) => value:
- type:
CONCAT
Adds values to end of state.- data:
state
|function<newState>(null, action, state)
- ...post-processors: see below
- data:
- type:
CONCAT_TO
Adds values to front of state.- data:
state
|function<newState>(null, action, state)
- ...post-processors: see below
- data:
- type:
POP
Remove value from end of state. - type:
SHIFT
Remove value from front of state. - type:
REMOVE_AT
Remove value from index in state.- index:
number
: index of removed value
- index:
- type:
SLICE
Remove value from slice of state.- start:
number
: start of slice defaults to0
- end:
number
: end of slice
- start:
- type:
MAP
Runs state throughlodash/map
.- data:
string
|function<value>(element, index, state)
: passed tolodash/map
- data:
- type:
FILTER
Runs state throughlodash/filter
.- data:
object
|function<boolean>(element, index, state)
: passed tolodash/filter
- data:
- type:
SET|SET_AT|CONACT|CONCAT_TO|INSERT_AT|PUSH|UNSHIFT
The following keys will post-process the state.- uniq:
boolean
: should the new state run throughlodash/uniq
default:false
- compact:
boolean
: should the new state run throughlodash/compact
default:false
- sort:
string | function<boolean>(element, index, state)
: passed tolodash/sortBy
default:undefined
(no sort)
- uniq:
const mySetArrayActionCreator = data => ({
type: 'SET_ARRAY',
data: ['new', 'data'],
});
// or
const mySetArrayActionCreator = data => ({
type: 'SET_ARRAY',
data: state => ['new', 'data'],
});
const myClearArrayActionCreator = data => ({
type: 'CLEAR_ARRAY',
});
const mySetAtActionCreator = data => ({
type: 'SET_AT_ARRAY',
index: 1,
data: oldVal => 'newVal',
});
const myInsertAtActionCreator = data => ({
type: 'INSERT_AT_ARRAY',
index: 1,
data: () => 'newVal',
});
const myPushActionCreator = data => ({
type: 'PUSH_ARRAY',
data: 'newVal at end',
});
const myUnshiftActionCreator = data => ({
type: 'UNSHIFT_ARRAY',
data: 'newVal at front',
});
const myConcatActionCreator = data => ({
type: 'CONCAT_ARRAY',
data: ['newVal 1 at end', 'newVal 2 at end'],
});
const myConcatToActionCreator = data => ({
type: 'CONCAT_AT_ARRAY',
data: ['newVal 1 at start', 'newVal 2 at start'],
});
const myPopActionCreator = data => ({
type: 'POP_ARRAY',
});
const myShiftActionCreator = data => ({
type: 'SHIFT_ARRAY',
});
const myRemoveAtActionCreator = data => ({
type: 'REMOVE_AT_ARRAY',
index: 1,
});
const mySliceActionCreator = data => ({
type: 'SLICE_ARRAY',
start: 1,
end: -1,
});
const myMapActionCreator = data => ({
type: 'MAP_ARRAY',
data: element => element,
});
const myFilterActionCreator = data => ({
type: 'FILTER_ARRAY',
data: element => !!element,
});
Returns a reducer that sets or clears a single value.
actionTypes
: object no single type is requiredUPDATE
: string the action type to update the stateSET
: string the action type to assign the stateCLEAR
: string the action type to clear the stateMERGE
: string the action type to merge the stateREPLACE
: string the action type to replace the stateFILTER
: string the action type to remove properties from the stateMAP_VALUES
: string the action type to map state valuesMAP_KEYS
: string the action type to map state keys
default
: object default state - will be reset to this state onCLEAR
defaults to {}
import { object } from 'easydux';
const myObjectReducer = object({
UPDATE: 'UPDATE_OBJECT',
SET: 'SET_OBJECT',
CLEAR: 'CLEAR_OBJECT',
MERGE: 'MERGE_OBJECT',
REPLACE: 'REPLACE_OBJECT',
FILTER: 'FILTER_OBJECT',
MAP_VALUES: 'MAP_VALUES_OBJECT',
MAP_KEYS: 'MAP_KEYS_OBJECT',
});
import { object } from 'easydux';
import { createStore } from 'redux';
const reducer = object({
SET: 'MY_OBJECT_SET',
});
const store = createStore(reducer);
store.dispatch({
type: 'MY_OBJECT_SET',
data: { hi: { mom: "I'm on TV!" } },
});
store.getState();
// { "hi": { "mom": "I'm on TV!" } }
store.dispatch({
type: 'MY_OBJECT_SET',
data: onWhere => onWhere.replace('TV', 'github'),
key: 'hi.mom',
depth: 2, // optional
});
store.getState();
// { "hi": { "mom": "I'm on github!" } }
- type:
UPDATE
Updates reducer state. See https://github.com/kolodny/immutability-helper- data:
config
|object
- data:
- type:
SET
Sets reducer state. Set behavior tries to imitateReact.Component().setState
- data:
state
|function<newState>(state, action)
- whatever is passed to the reducer is merged with the state one level deep.
- If a function is passed to data, state will be passed as that function's first argument and the result will be merged with the current state.
- whatever is passed to the reducer is merged with the state one level deep.
- depth:
number
: the depth of the merge data will have with original state. defaults to1
- key: see below
- data:
- type:
MERGE
Merges reducer state. Useslodash/merge
.- data:
state
|function<newState>(state, action)
- key: see below
- data:
- type:
REPLACE
Replaces reducer state.- data:
state
|function<newState>(state, action)
- key: see below
- data:
- type:
MAP_VALUES
Runs state throughlodash/mapValues
.- data:
function<value>(element, key, state)
: passed tolodash/mapValues
- key: see below
- data:
- type:
MAP_KEYS
Runs state throughlodash/mapKeys
.- data:
function<value>(element, key, state)
: passed tolodash/mapKeys
- key: see below
- data:
- type:
CLEAR
Sets reducer to default. - type:
FILTER
Runs state throughlodash/omit
orlodash/omitBy
.- data:
array
|function<boolean>(element, index, state)
: passed tolodash/omit
for arrays andlodash/omitBy
for functions - key: see below
- data:
const mySetObjectActionCreator = data => ({
type: 'SET_OBJECT',
data: { hello: 'world' },
});
// or
const mySetObjectActionCreator = data => ({
type: 'SET_OBJECT',
data: state => ({ hello: 'world' }),
});
const myMergeObjectActionCreator = data => ({
type: 'MERGE_OBJECT',
data: state => ({ hello: 'world' }),
});
const myReplaceObjectActionCreator = data => ({
type: 'REPLACE_OBJECT',
data: state => ({ hello: 'world' }),
});
const myClearObjectActionCreator = data => ({
type: 'CLEAR_OBJECT',
});
const myFilterActionCreator = data => ({
type: 'FILTER_OBJECT',
data: element => !!element,
});
- key:
string
|array
: A key to target in the reducer state.
The same kind of argument you would pass to lodash/get
. key
contextualizes your action around a certain value in an object - passing the value of this key in the object to your data function and then setting the data (or the result of the data function) at the key in the object.
const reducer = object({
SET: 'MY_OBJECT_SET',
});
const store = createStore(reducer, {
hello: {
my: 'honey',
},
});
store.dispatch({
type: 'MY_OBJECT_SET',
data: ({ my }) => ({ my: 'baby' }),
key: 'hello',
})
store.getState();
// { hello: { my: 'baby' } }
// or
store.dispatch({
type: 'MY_OBJECT_SET',
data: honey => 'baby', // or just data: 'baby'
key: 'hello.my', // or ['hello', 'my']
})
store.getState();
// { hello: { my: 'baby' } }
easydux
is free software under the MIT license.