rereduce - Reducer library for Redux

jopen 8年前發布 | 11K 次閱讀 rereduce

Rereduce

Simple reducer library for Redux. It's like Reselect but for reducers.

Reducers can depend on each others.

It permits to replace the imperative waitFor of original Flux implementation by a purely functional approach.

/!\ Work in progress: Current implementation does not play well (yet) with server-side rendering or time-travel.

API

createReducer( [reducerDependencies], reducerFunction)

[reducerDependencies] is an optional object that looks like {key1: reducer1, key2: reducer2}

If dependencies are provided, reducerFunction will be called with a 3rd argument with the state of the dependency reducers.

Note that the API is a first draft and may change in the future. Don't hesitate to discuss what the API should look like here: https://github.com/slorber/rereduce/issues/1

Simple example

import {  createReducer  } from 'rereduce'

// Create a simple reducer without dependencies const firstReducer = createReducer(function(state = 0, action) { switch (action.type) { case 'increment': return state + 1 case 'decrement': return state - 1 default: return state } })

// Create another reducer that depends on first reducer const secondReducer = createReducer({ firstReducer }, // declare dependency to firstReducer function(state = 0,action,{firstReducer}) { return firstReducer + 1 } )

// Everytime secondReducer is called, firstReducer will also be called // firstReducer is memoized so it is not inefficient to call it multiple times</pre> </div>

How it works

What it does in reality: