• Stars
    star
    1,007
  • Rank 45,632 (Top 0.9 %)
  • Language
    JavaScript
  • Created over 9 years ago
  • Updated almost 9 years ago

Reviews

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

Repository Details

RxJS utilities for Redux.

redux-rx

build status npm version

RxJS utilities for Redux. Includes

  • A utility to create Connector-like smart components using RxJS sequences.
    • A special version of bindActionCreators() that works with sequences.
  • An FSA-compliant observable middleware
  • A utility to create a sequence of states from a Redux store.
npm install --save redux-rx rx

Usage

import { createConnector } from 'redux-rx/react';
import { bindActionCreators, observableMiddleware, observableFromStore } from 'redux-rx';

createConnector(selectState, ?render)

This lets you create Connector-like smart components using RxJS sequences. selectState() accepts three sequences as parameters

  • props$ - A sequence of props passed from the owner
  • state$ - A sequence of state from the Redux store
  • dispatch$ - A sequence representing the dispatch() method. In real-world usage, this sequence only has a single value, but it's provided as a sequence for correctness.

selectState() should return a sequence of props that can be passed to the child. This provides a great integration point for sideways data-loading.

Here's a simple example using web sockets:

const TodoConnector = createConnector((props$, state$, dispatch$) => {
  // Special version of bindActionCreators that works with sequences; see below
  const actionCreators$ = bindActionCreators(actionCreators, dispatch$);
  const selectedState$ = state$.map(s => s.messages);

  // Connect to a websocket using rx-dom
  const $ws = fromWebSocket('ws://chat.foobar.org').map(e => e.data)
    .withLatestFrom(actionCreators$, (message, ac) =>
      () => ac.receiveMessage(message)
    )
    .do(dispatchAction => dispatchAction()); // Dispatch action for new messages

  return combineLatest(
    props$, selectedState$, actionCreators$, $ws,
    (props, selectedState, actionCreators) => ({
      ...props,
      ...selectedState,
      ...actionCreators
    }));
});

Pretty simple, right? Notice how there are no event handlers to clean up, no componentWillReceiveProps(), no setState. Everything is just a sequence.

If you're new to RxJS, this may look confusing at first, but β€” like React β€” if you give it a try you may be surprised by how simple and fun reactive programming can be.

TODO: React Router example. See this comment for now.

render() is an optional second parameter which maps child props to a React element (vdom). This parameter can also be a React Component class β€” or, if you omit it entirely, a higher-order component is returned. See createRxComponent() of react-rx-component for more details. (This function is a wrapper around that library's createRxComponent().)

Not that unlike Redux's built-in Connector, the resulting component does not have a select prop. It is superseded by the selectState function described above. Internally, shouldComponentUpdate() is still used for performance.

NOTE createConnector() is a wrapper around react-rx-component. Check out that project for more information on how to use RxJS to construct smart components.

bindActionCreators(actionCreators, dispatch$)

This is the same, except dispatch$ can be either a dispatch function or a sequence of dispatch functions. See previous section for context.

observableMiddleware

The middleware works on RxJS observables, and Flux Standard Actions whose payloads are observables.

The default export is a middleware function. If it receives a promise, it will dispatch the resolved value of the promise. It will not dispatch anything if the promise rejects.

If it receives an Flux Standard Action whose payload is an observable, it will

  • dispatch a new FSA for each value in the sequence.
  • dispatch an FSA on error.

The middleware does not subscribe to the passed observable. Rather, it returns the observable to the caller, which is responsible for creating a subscription. Dispatches occur as a side effect (implemented using doOnNext() and doOnError()).

Example

// fromEvent() used just for illustration. More likely, if you're using React,
// you should use something rx-react's FuncSubject
// https://github.com/fdecampredon/rx-react#funcsubject
const buttonClickStream = Observable.fromEvent(button, 'click');

// Stream of new todos, with debouncing
const newTodoStream = buttonClickStream
  .debounce(100)
  .map(getTodoTextFromInput);

// Dispatch new todos whenever they're created
dispatch(newTodoStream).subscribe();

observableFromStore(store)

Creates an observable sequence of states from a Redux store.

This is a great way to react to state changes outside of the React render cycle. See this discussion for an example. I'll update with a proper example once React Router 1.0 is released.

Also, I'm not a Cycle.js user, but I imagine this is useful for integrating Redux with that library.

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

react-remarkable

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

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
7

realm

JavaScript
170
star
8

redux-transducers

Transducer utilities for Redux.
JavaScript
130
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