Get the reducer. You will need to add this to your store.
Parameters
defaultState
Object
Examples
const reducer = createReducer(defaultState);
const rootReducer = combineReducers({app: reducer});
Returns Function
Create a paths object that contains all the paths to your state properties. This is especially useful when using TypeScript, because it adds type checking to the paths given to updaters.
Parameters
defaultState
Object
Examples
const paths = createStatePath(defaultState)
dispatch(update(paths.pager.currentIndex, 3)) // this will result in a TypeScript error if pager.currentIndex is not in your defaultState.
Returns StatePathTree
Parameters
Examples
dispatch(update('currentIndex', 3))
dispatch(update('currentIndex', state => state.app.pages.length - 1))
Toggle a boolean value
Parameters
statePath
StatePath
Examples
dispatch(toggle('menu.isOpen'))
Increment a value by x where x is an optional parameter ('by') that defaults to 1. There is also an optional max parameter: if the result of the incrementation would result higher than that max value, it sets the value to the max value instead.
Parameters
statePath
StatePathby?
number By how much to increase the value. Defaults to 1.max?
number Set an upper limit on the result value.
Examples
dispatch(increment('currentPage', 5, lastPage))
Decrement a value by x where x is an optional parameter ('by') that defaults to 1 There is also an optional min parameter, if the result of the incrementation would result lower than that min value, it sets the value to the min value instead
Parameters
statePath
StatePathby?
numbermin?
number
Examples
dispatch(decrement('currentPage', 5, 0))
Add something to the end of an array.
Parameters
statePath
StatePathvalue
any
Examples
dispatch(arrayAdd('todos', 'New to do'));
Merge objects with an optional depthLimit, deepmerge everything under the depthLimit, shallow merge the rest. Arrays are always merged using a shallow merge. A depthLimit of 1 means "do a shallow merge", meaning objects within the specified object will be replaced. A depthLimit of 2 means "shallow merge objects within this object". And so on.
Parameters
statePath
StatePathobject
objectdepthLimit?
number Set to 1 for a shallow merge.
Examples
dispatch(objectMerge('myObject', object));
Update a value within an array by index or by predicate function. If a predicate function is given, all values for which the function returns true will be replaced with the given value.
Parameters
statePath
StatePathindexOrFn
number | (value: any, index: number) => booleannewValue
Examples
dispatch(arrayReplace('todos', todoIndex, myTodo));
dispatch(arrayReplace('todos', (todoItem, index) => todoItem.id === todoId, myTodo);
Remove an entry from an array. When a number is given, removes the entry at that index. When a function is given, removes items for which the function returns true.
Parameters
statePath
StatePathindexOrFn
number | (value: any, index: number) => boolean
Examples
dispatch(arrayRemove('app.todos', todoIndex));
dispatch(arrayRemove('app.todos', (todoItem, index) => todoItem.id === todoId));
Resets the given state path to the initial state given in the createReducer function.
Parameters
statePath
StatePath
Examples
dispatch(reset('app.todos'));
These types and interfaces can be imported from the package when using TypeScript.
A tree that contains paths to your state properties, derived from your default state. Useful when using TypeScript.
Type: Object
A path representation to a property in the state, e.g. pager.currentIndex
.
Type: string | StatePathTree<any>