Home

Awesome

redux-either

build status npm version

FSA-compliant either monad middleware for Redux.

This is based on redux-future.

npm install --save redux-either

Usage

import eitherMiddleware from 'redux-either';
import { Either } from 'ramda-fantasy';
const store = createStore(
  reducer,
  initialState,
  applyMiddleware(
    eitherMiddleware(
      Either, // the Either instance
      Either.either // function to get the value from the Either
    )
  )
)()

// with data.either from folktale
import eitherMiddleware from 'redux-either';
import { Either } from 'ramda-fantasy';
const store = createStore(
  reducer,
  initialState,
  applyMiddleware(
    eitherMiddleware(
      Either, 
      (l, r, e) => e.fold(l, r)
    )
  )
)()

Example

import Either from 'data.either';

store.dispatch({
  type: 'ADD'
, either: Either.Right(2) // <= none FSA needs the either keyword
});

Using in combination with redux-actions

Because it supports FSA actions, you can use redux-either in combination with redux-actions.

Example: Action creators

const action = createAction('FSA_ACTION');
store.dispatch(action(R.map(R.toUpper, Either.Right('test'))));
// => payload will be 'TEST'

store.dispatch(action(R.map(R.toUpper, Either.Left('error'))));
// => error will be 'error'

Example: Future(Either)

You can use redux-either together with redux-future.

// futureEither :: Future(Either(String))
const futureEither = new Future((reject, resolve) => {
  fetch('users', (err, res) =>
    err
      ? reject(err)
      : res.length <= 0
        ? resolve(Either.Left('no users found'))
        : resolve(Either.Right(res)));
});

const action = createAction('FSA_ACTION');
store.dispatch(action(futureEither));

Related

What's an Either?

Libraries