- counter
- toggle
- value
- set-simple
- task-simple
- list
State structure
type ValueType = any; // prefer serializable types
// value state example
"Batman";
// reducers.js
import { combineReducers } from "redux";
import { createReducer } from "power-reducers/value";
import { SET_TOKEN, LOGOUT, INVALIDATE_TOKEN } from "./actions";
const [token] = createReducer({
initial: '',
setOn: { type: SET_TOKEN, payload: 'token' } // data from action.token
resetOn: [LOGOUT, INVALIDATE_TOKEN]
// other parameters
});
export default combineReducers({
token
});
[
reducerFunction, // (state, action) => newState
{
getInitialState, // () => initialState
generateState // (value: any) => valueState
}
] = createReducer(/* ... */);
Initial/default value .
type: any
default: ''
(empty string)
What action(s) will setting value
// action example:
{ type: "SET_TOKEN", payload: 'token' }
What action(s) will set value that was set as initial
// action example:
{ type: 'RESET_TOKEN', ... }
Create own reducers for different action(s) types (try to avoid this).
Example
createReducer({
// ...
_customHandlers: [
{
type: "SOME_ACTION",
handler: (state, action) => {
// return new state for action.type === "SOME_ACTION"
}
}
]
});
1 type HandlerOption - single item or Array containing the following types (can be mixed):
Parameter example | Valid action example |
---|---|
"SET_VALUE" |
{ type: "SET_VALUE", payload: /* some data */ } |
{ type: "SET_VALUE" } |
{ type: "SET_VALUE", payload: /* some data */ } |
{ type: "SET_TOKEN", payload: "token" } |
{ type: "SET_TOKEN", token: /* some data */ } |
{ type: "SET_TOKEN", payload: (action) => someData |
{ type: "SET_TOKEN", /* data to be resolved */ } |