• Stars
    star
    130
  • Rank 277,575 (Top 6 %)
  • Language
    JavaScript
  • Created over 9 years ago
  • Updated about 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Transducer utilities for Redux.

redux-transducers

build status npm version

Transducer utilities for Redux.

  • transducerProtocol lets you dispatch using transducers.
  • transduce() lets you create reducers from transducers.

Conforms to the transducer protocol used by transducers.js and transducers-js, and is tested against those libraries.

npm install --save redux-transducers

Using transducers to dispatch actions

transducerProtocol(createStore)

This is a higher-order store that enables a Redux store to be dispatched via a transducer. Higher-order stores aren't currently documented (it's coming) but they're simple to use:

const newCreateStore = transducerProtocol(createStore);
const store = newCreateStore(reducer, initialState);

That's it! Now you can dispatch actions to your stores using transducers.

NOTE: If you're using other higher-order stores, like the forthcoming applyMiddleware(), transducerProtocol must come first in the chain. This is because, in order to conform to the transducer protocol, and for compatibility with popular transducer libraries, the store returned by transducerProtocol() is not a plain object. This shouldn't be a problem. Just remember to always put first.

// This won't work
const newCreateStore = compose(applyMiddleware(m1, m2, m3), transducerProtocol, createStore);
// Do this instead
const newCreateStore = compose(transducerProtocol, applyMiddleware(m1, m2, m3), createStore);

How it works

The best way to explain this is probably just to show you an example:

Example: mapping strings to actions

// Using the transducers.js library
const actions = [
  'Use Redux',
  'Weep with joy',
  'Mutate inside the reducer',
  null,
  'Learn about higher-order stores',
  { type: 'REMOVE_TODO', payload: 2 },
  'Learn about middleware'
];

into(store, compose(
  keep(),
  map(a => typeof a === 'string'
    ? { type: 'ADD_TODO', payload: { text: a } }
    : a
  ),
  filter(a => !(
    a.type === 'ADD_TODO' &&
    /(M|m)utat(e|ion)/g.test(a.payload.text)
  ))
), actions);

This example uses the into(to, xform, from) function of transducers.js. It applies a transformation to each action in a collection — in this case an array, but could be any iterable data structure — and "pours" it into the target collection — in this case, a store — by performing a dispatch. The call to store.dispatch() is analogous to a call to array.push().

Using transducers to create reducers

transduce(xform, reducer)

transduce() creates a reducer from a transducer and a base reducer. The transformation is applied before being sent to the base reducer.

Caveat: transduce() does not support stateful transducers

Transducers typically operate on collections. It's possible to use transducers to transform asynchronous streams, but it requires the use of local state that persists over time. We can't do this, because Redux makes a hard assumption that the reducer is a pure function — it must return the same result for a given state and action, every time.

For this reason, transduce() transforms actions one at a time. That means transducers like filter() and map() work fine, but take() and dedupe() do not.

This caveat does not apply to transducerProtocol(), which works with all transducers, stateful or otherwise, because it does its transforms before they reach the reducer.

Example: filtering action types

import { filter } from 'transducers.js';
import transduce from 'redux-transducers';

const addTodoReducer = transduce(
  filter(action => action.type === 'ADD_TODO'),
  (state, action) => ({ ...state, todos: [...state.todos, action.payload })
);

const removeTodoReducer = transduce(
  filter(action => action.type === 'REMOVE_TODO'),
  (state, action) => ({ ...state, todos: state.todos.filter(t => t.id !== action.payload.id) })
);

// Combine into a single reducer with reduce-reducers
// https://github.com/acdlite/reduce-reducers
import reduceReducers from 'reduce-reducers';
const todoReducer = reduceReducers(addTodoReducer, removeTodoReducer);

More Repositories

1

recompose

A React utility belt for function components and higher-order components.
JavaScript
14,760
star
2

react-fiber-architecture

A description of React's new core algorithm, React Fiber
11,620
star
3

redux-router

Redux bindings for React Router – keep your router state inside your Redux store
JavaScript
2,303
star
4

flummox

Minimal, isomorphic Flux.
JavaScript
1,689
star
5

redux-rx

RxJS utilities for Redux.
JavaScript
1,007
star
6

react-remarkable

A React component for rendering Markdown with remarkable
JavaScript
450
star
7

redux-batched-updates

Batch React updates that occur as a result of Redux dispatches, to prevent cascading renders. See https://github.com/gaearon/redux/issues/125 for more details.
JavaScript
222
star
8

realm

JavaScript
170
star
9

relay-sink

Use Relay to fetch and store data outside of a React component
JavaScript
125
star
10

json-sass

Transforms a JSON stream into scss syntax Sass.
JavaScript
94
star
11

suspense-ssr-demo

JavaScript
93
star
12

flummox-isomorphic-demo

Demo of how to create isomorphic apps using Flummox and react-router
JavaScript
89
star
13

react-rx-component

Yet another RxJS library for React :) Create container components (also known as smart components) by transforming a sequence of props
JavaScript
82
star
14

jquery.sidenotes

Transform Markdown footnotes into superpowered sidenotes
CoffeeScript
73
star
15

realm-redux

JavaScript
58
star
16

change-emitter

Listen for changes. Like an event emitter that only emits a single event type. Really tiny.
JavaScript
57
star
17

the-react-way

An isomorphic React slide deck, about React.
JavaScript
47
star
18

flpunx

Better than all the other Flux libraries combined!
JavaScript
35
star
19

router

An experiment in functional routing for JavaScript applications.
JavaScript
27
star
20

react-suitcss

Build React components that conform to SUIT CSS naming conventions.
JavaScript
24
star
21

flummox-immutable-store

Flummox store with Immutable.js support for serialization and undo/redo
JavaScript
17
star
22

react-object-fit-cover

A React component that mimics object-fit: cover
JavaScript
12
star
23

sassy-namespaces

Namespaces in Sass, minus the headaches.
CSS
10
star
24

redux-basic-blog-example

JavaScript
6
star
25

react-media-mixin

A React mixin to update state in response to media query events.
JavaScript
6
star
26

andrewphilipclark.com

My personal website/blog
CSS
4
star
27

babel-plugin-react-pure-component

JavaScript
3
star
28

react-focuspoint

A React component for 'responsive cropping' with jQuery FocusPoint.
JavaScript
2
star
29

anthem

Minimal REST framework
JavaScript
2
star
30

todos

JavaScript
1
star