Given the path of a certain state slice, returns a function that can be used to create state selectors (helpful for mapStateToProps()
).
slicePath
String Path to slice of state.
import { selectorForSlice } from 'lp-redux-utils'
const state = {
userSlice: {
user: {
name: 'test'
}
}
}
const select = selectorForSlice('userSlice')
// The resulting select() function has arguments (path, defaultValue)
const selectors = {}
selectors.user = select('user')
selectors.username = select('user.name', 'defaultName')
export { selectors }
// These selectors can be called in mapStateToProps() like so:
// selectors.user(state) => { name: 'test' }
// selectors.username(state) => 'test'
*
Returns Function A function that can be used to create state selectors.
A helper function for creating simple "setter" reducers. Given a path, it sets the state at that path to the payload of an action.
path
String Path to the part of the state that will be settransform
Function A function with arguments(action, state, slice)
that can be used to transform the value that will be set.slice
is the data, if any, that already exists in the state atpath
. The default transform function simply returns the action's payload. To set the state to a constant value, simply pass the value in place of the transform function.
import { setState } from 'lp-redux-utils'
import { createAction, handleActions } from 'redux-actions'
const setCount = createAction('SET_COUNT')
const setCountInverse = createAction('SET_COUNT_INVERSE')
const doubleExistingCount = createAction('DOUBLE_EXISTING_COUNT')
const reducer = handleActions({
[setCount]: setState('count'),
[setCountInverse]: setState('count', action => action.payload * -1)
[doubleExistingCount]: setState('count', (action, state, count) => count * 2)
[resetCount]: setState('count', 0)
})
*
Returns Function A function that can be used in a reducer to handle an action.
A helper function for creating simple reducers that "unset" a piece of state. Given a path, it calls lodash unset on the state at that path.
path
String Path to the part of state to unset
import { unsetState } from 'lp-redux-utils'
import { createAction, handleActions } from 'redux-actions'
const clearCount = createAction('CLEAR_COUNT')
const reducer = handleActions({
[clearCount]: unsetState('count'),
})
*
Returns Function A function that can be used in a reducer to handle an action.