warped-reducers

Compile a standard Redux reducer from a brief definition

Usage no npm install needed!

<script type="module">
  import warpedReducers from 'https://cdn.skypack.dev/warped-reducers';
</script>

README

Warped Reducers

Build Status Greenkeeper Enabled

Compile a standard Redux reducer from a brief definition.

Usage in Node depends on --experimental-modules. With older Node versions, use esm.

API

createReducer :: String -⁠> StrMap (b -⁠> a -⁠> a) -⁠> { types :: StrMap String, actions :: StrMap (b -⁠> { type :: String, payload :: b }), reducer :: (a, b) -⁠> a }

This is also the default export from this module.

Given a String representing the namespace, and a StrMap of action handlers, returns a Record containing a single reducer, and a new StrMap mapping the original keys to canonical identifiers of the types that the reducer can handle.

An action handler is a curried function that takes the payload of the action first, and the state second, and should return a new state. We recommend usings Optics, such as the lens-related functions from Ramda, to define the reducers - the signature of action handlers is designed to align perfectly with the signature of functional utilities.

const setMyProp = myProp => state => Object.assign ({}, state, {myProp});
createReducer ('MyNamespace') ({setMyProp});

noopAction :: a -⁠> b -⁠> b

A conviently named function that does nothing to your state.

To be used when you need to define an action type which should not affect the state, but can be used as a message to your Redux side-effect handling middleware.

> noopAction ({do: 'nothing'}) ({myState: 'whatever'})
{myState: 'whatever'}