Skip to content

Latest commit

 

History

History
116 lines (85 loc) · 2.42 KB

value.md

File metadata and controls

116 lines (85 loc) · 2.42 KB

power-reducers


value

State structure

type ValueType = any; // prefer serializable types
// value state example
"Batman";

createReducer

USAGE (example with redux)

// 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
});

RETURNS

[
  reducerFunction, // (state, action) => newState
  {
    getInitialState, // () => initialState
    generateState // (value: any) => valueState
  }
] = createReducer(/* ... */);

PARAMETERS

initial

Initial/default value .

type: any

default: '' (empty string)

setOn1

What action(s) will setting value

// action example:
{ type: "SET_TOKEN", payload: 'token' }

resetOn1

What action(s) will set value that was set as initial

// action example:
{ type: 'RESET_TOKEN', ... }

_customHandlers

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 */ }