1.0.12 • Published 2 years ago
redux-data-module v1.0.12
Base Module
Options
moduleKey
: string (required)
This key is used to generate the action key prefix in case it was not specified.
e.g.: mymodule
, mymodule/submodule
reducerKey
: string (optional, defaults to moduleKey
)
This key is used to retrieve the module state starting from the root of the state. It supports nested keys.
e.g.: mymodule
, mymodule.submodule
initialState
: object (optional)
This object represents the initial state of the module.
actionKeyPrefix
: string (optional)
This key is used to generate a prefix for action keys. It can be any string you want. If omitted, it will be generated from the moduleKey
.
registerSelector(selectorName, selectState, selector)
This function is used to register a selector on the module.
selectorName
: string Name of the selectorselectState
: func | func This function receives the root state and the module state as parameters. This function must return a value.selector
: func This function receives the values returned inselectState
in the order they were called. This function must return a value
registerActionKey(actionKey)
This function is used to register an action key, using the module's actionKeyPrefix
.
actionKey
: string
Examples
Creating a counter that keeps track of a value and let's us increment and decrement it.
const CounterModule = new BaseModule({
moduleKey: 'counter',
initialState: {
value: 0
}
})
Counter.registerSelector('value', (state, moduleState) => moduleState.value, (value) => value)
Counter.registerActionKey('increment')
Counter.actions.increment = () => dispatch => dispatch({ type: Counter.actionKeys.increment })
Counter.reducers[Counter.actionKeys.increment] = (state) => ({ ...state, value: state.value + 1 })
Counter.registerActionKey('decrement')
Counter.actions.decrement = () => dispatch => dispatch({ type: Counter.actionKeys.decrement })
Counter.reducers[Counter.actionKeys.decrement] = (state) => ({ ...state, value: state.value - 1 })