redux-binary
Create simple Redux actions and a reducer for managing binary state.
Install
$ npm install redux-binary
Usage
Use reduxBinary
to create a reducer and actions for state that can take on
only two values. The default binary values are false
and true
, with the
initial state being set to whatever the "off" value is. In this case, that would
be the default value false
. reduxBinary
returns an object containing the
reducer, binary actions, and binary action type names.
The first argument is a suffix that will be used to generate the action type
names. The action type names are generated by appending 'ON_'
and 'OFF_'
to
the suffix and uppercasing the whole action type name. The returned action types
are keyed with 'ON'
and 'OFF'
.
; const suffix = 'editing';const actionTypes = actionTypesON === 'ON_EDITING';actionTypesOFF === 'OFF_EDITING';
The returned action creators, keyed by actions
, are named by the second and
third arguments. The second argument is the name of the "on" action creator, and
the third argument is the name of the "off" action creator.
; const onActionName = 'edit';const offActionName = 'stopEditing'; const actionTypes actions = ; actionsonActionName === actionsedit;actionsoffActionName === actionsstopEditing; actions; // { type: actionTypes.ON };actions; // { type: actionTypes.OFF };
The reducer only handles the two generated actions. As you might guess, the reducer flips the state from "off" to "on" and vice-versa. If the state is already "on", then the "on" action does not change it. The same idea applies to the "off" state.
; const reducer actions = ; === false; // Initial default state of false === true; === true; === false; === false;
You can define what the binary state is by supplying objects to the second and third arguments as well. Pass in a single key-value object where the key is the action creator name and the value is the state for that particular argument number (i.e. the value inside second object argument would be the "on" state). In this case, the initial state will be equal to whatever your "off" state is.
; const onActionDef = edit: 1 ;const offActionDef = stopEditing: 0 ; const reducer actions = ; === 0; // Initial "off" state of 0 === 1; === 1; === 0; === 0;
If you don't want your initial state to be "off", then you can also pass in an object for the first argument, the suffix. Similar to the object for defining your action creators and binary state, this object must be a single key-value object where the key is the suffix name and the value is the initial state. NOTE: the initial state must be one of the binary state values.
Example with default state values:
; const suffixDef = editing: true ; const reducer actions = ; === true; // Initial value of true
Example with custom state values:
; const suffixDef = editing: true ;const onActionDef = edit: 1 ;const offActionDef = stopEditing: 0 ; const reducer actions = ; === 1; // Initial value of 1
Example with improper initial state value:
; // These will throw
It goes without saying, but your "on" and "off" action creator names and custom state values can't be the same either:
; // These will throw;
API
type OnValue = any;type OffValue = any;type State = OnValue | OffValue;type Action = type: string ; : State; actionTypes: ON: string; OFF: string ; actions: `ON_`: Action `OFF_`: Action ;