RxReact
Installation
Install this module with npm:
npm install rx-react
Usage:
RxReact provides a set of utilities to work with RxJS and React :
- The
StateStreamMixin
- The
LifecycleMixin
- The
PropsMixin
- The
RxReact.Component
base class - The
FuncSubject
helper
StateStreamMixin
The StateStreamMixin
allows to bind a component state to an RxJS Observable
stream.
The way to achieve the binding is to provide a getStateStream
method on your component that returns an RxJS Observable
, the StateStream
mixin will automatically merge the state of your component with the values published by the returned observable. The subscription will be automaticly cleaned on component unmount.
Example:
var StateStreamMixin = StateStreamMixin;var React = ;var Rx = ; var Timer = React; React;
LifecycleMixin
The LifecycleMixin
allows you to consume React components lifecycle events as RxJS Observable
.
The LifecycleMixin
will inject a property lifecycle
to the component, that property contains an observable for each lifecycle events.
Example :
var LifecycleMixin = LifecycleMixin;var React = ;var Rx = ; var Component = React;
PropsMixin
The PropsMixin
allows to obtain a stream of props as RxJS Observable
for your component.
Example :
var PropsMixin = PropsMixin;var React = ; var Component = React
This is particulary useful in combination with the StateStreamMixin
when your component states depends on Props.
Component
The RxReact.Component
is a base class combining the behavior of the PropsStreamMixin
and the StateStreamMixin
.
It extends React.Component
.
Example:
var RxReact = ;var Rx = ; Component { return RxObservable; } { var secondsElapsed = thisstate ? thisstatesecondsElapsed : 0; return <div>Seconds Elapsed: secondsElapsed</div> ; }
Note that when you extend lifecycle methods, you must call the super
method.
Before the 0.3.x versions
RxReact.Component
also implemented lifecyle mixin behavior, for some perf reasons and because most of the time it's unnecessary this has been removed. If you want reenable this behavior useFuncSubject
as lifecycle method, or manually apply theLifecycleMixin
on your class.
FuncSubject
The FuncSubject
helper allows to create an RxJS Observable
that can be injected as callback for React event handlers, refs, etc...
To create an handler use the create
function of FuncSubject
var myHandler = FuncSubject
Example:
var FuncSubject = FuncSubject;var React = ;var Rx = ; var Button = React;
FuncSubject
also accept a function as argument, if provided this funtion will be used to map the value of each elements.
This function will always be called even if the FuncSubject
has no subscription.
var FuncSubject = FuncSubject;var React = ;var Rx = ; var MyComponent = React;
FuncSubject.behavior
You can also create a FuncSubject
that extends BehaviorSubject
. simply use the behavior
function exposed by FuncSubject
:
var subject = FuncSubject
FuncSubject.async
You can also create a FuncSubject
that extends AsyncSubject
. simply use the async
function exposed by FuncSubject
:
var subject = FuncSubject
FuncSubject.replay
You can also create a FuncSubject
that extends ReplaySubject
. simply use the replay
function exposed by FuncSubject
:
var subject = FuncSubject
FuncSubject.replay
You can create a FuncSubject
from any subject base class using the factory
function expsed by FuncSubject
:
var subject = FuncSubject;