Using redux state
Creating states
#
Lets create a new state in the redux store at the path named todos
with initial values of array {
1: {done: false},
2: {done: true},
3: {done: false}
}
.
#
Controlling how initial state should be setWhen you use the redux-state-hook
with a default state, under the hood it sets the initial state in the store with the given path.
redux-state-hook
has an intelligent setter function which determines how your state will be set based on the payload (referring the initial state as the payload).
When array is passed as the payload setter assumes you will append the payload with the current state array.
Given the example below the initial todos state has [{done: false}, {done: true}, {done: false}]
value, setter will push to the existing array state if the payload is array otherwise will replace the state.
Same logic applies to json object but primitive payload value replaces the existing state.
You can handle the setter behaviour by passing a reducer function.
Retrieving state
#
Lets retrieve the new state we have created using getState.
#
Reactively getting stateWe can retrieve states reactively using useStateSelector or useGetState hooks.
#
Getting already existing statesIn some cases we might want to get an already existsing state without using the useReduxState.
#
Reactively getting already existing statesWe can reactively get already existsing state without using the useReduxState.
#
Retrieving computed state with callbackWe can retrieve and compute states by passing callback function to useStateSelector hook and or getState.
Updating states
#
We can update states with setState.
#
Updating states with setter callbackWe can also update states by passing a custom setter to setState.
#
Updating already existing statesWe can update already existing states with useSetState hook.
#
Updating already existing statesWe can update already existing states with useSetState hook.