Skip to content

rvikmanis/redux-fp

Repository files navigation

redux-fp

Functional programming helpers for Redux.

Build Status Code Coverage

Table of contents

Updaters vs. reducers

Updater is a type of function that takes an action and returns another function that takes the state and produces a result.

action => state => {
  // Do something
}

Yes, they are just curried reducers with reversed argument order.

Using with utility libraries

Unlike reducers, updaters compose nicely with existing tools like lodash/fp or Ramda.

Let's create an updater that replaces a part of the state with the action's payload. We will use lodash/fp's set helper to illustrate the point.

import { set } from 'lodash/fp'
const changeFoo = action => set('foo', action.payload)

Note that I've eliminated state from the definition of our updater, and called set with two, not three, arguments. This works because when set is called without the third argument, it returns a function that takes the missing argument and produces a result. That function becomes the inner function of our updater and receives the state.

The paradigm is called point-free style, and if you're not familiar with it, check out Lucas Reis' introductory article.

Compatibility

To create a Redux store, just wrap the root updater in a reducer:

createStore((s, a) => updater(a)(s))

Purpose

redux-fp provides a set of useful utilities and enhancers like combine, withDefaultState or mapState that help working with updaters.

Check out the API reference for a full list of helpers, complete with descriptions and usage examples.

Installation

Node

npm i --save redux-fp

Or as a development dependency:

npm i --save-dev redux-fp

Browser

<script src="https://unpkg.com/redux-fp/dist/redux-fp.min.js"></script>

Makes the library available globally as ReduxFp.

API reference

See docs/API.md

Releases

No releases published

Packages

No packages published