cycle-react
An RxJS functional interface
to Facebook's React.
cycle-react allows users to write React
applications in functional style and represents their UIs as Observables.
In addition, cycle-react is immutable and uses
PureRenderMixin
internally by default.
Additionally, cycle-react is also a React-style implementation of a beautiful
framework called Cycle.js.
Installing
npm install cycle-react
Example
let Cycle = require('cycle-react');
let React = Cycle.React;
function computer(interactions) {
return interactions.get('.myinput', 'input')
.map(ev => ev.target.value)
.startWith('')
.map(name =>
<div>
<label>Name:</label>
<input className="myinput" type="text"></input>
<hr />
<h1>Hello {name}</h1>
</div>
);
}
Cycle.applyToDOM('.js-container', computer);
The input of the computer
is interactions
, a collection containing all
possible user interaction events happening on elements on the DOM, which you
can query using interactions.get(selector, eventType)
.
The output of the computer
is Observable<ReactElement>
(a reactive sequence of elements, in other words, view).
Function applyToDOM
subscribes that Observable of elements and renders the
elements to DOM, by using React.createClass
and React.render
internally.
Notice that although React.createClass
is mentioned here, you don't have to
use it. That's why cycle-react was made. We took functions over classes
and mutable states.
The description of the concept behind applyToDOM
and Cycle
can be found at
Cycle.js README.
Custom element example
let Cycle = require('cycle-react');
let React = Cycle.React;
let Rx = Cycle.Rx;
let CounterText = Cycle.createReactClass('CounterText',
function (interactions, props) {
return props.get('counter')
.map(counter => <h3>Seconds Elapsed: {counter}</h3>);
}
);
let Timer = Cycle.createReactClass('Timer', function () {
return Rx.Observable.interval(1000).map(i =>
<CounterText counter={i}></CounterText>
);
});
Cycle.applyToDOM('.js-container', Timer);
You can use h
and without JSX just like you did in Cycle.js.
This was made possible by
react-hyperscript.
The example.
But you said no classes
createReactClass
transforms your computer()
function into a ReactClass. So,
you get a ReactClass but without writing a class definition. The point is that
ReactClass is a function indeed and it should always be used as a
function object, because you don't new
, extends
or this
to access
properties. In fact, we don't want you to do that.
Apps written in cycle-react are this
-less. You won't find a single this
in the examples.
Learn more
cycle-react shares the same API as Cycle.js, except of doing custom elements.
A more comprehensive README can be found at
https://github.com/staltz/cycle
FAQ
Yes. And no extra configuration needed.
Example
Can I use cycle-react with other React components and libraries?
Yes. You can even use cycle-react with your current React apps. Because
createReactClass
creates the native ReactClass for you.
Examples for integrating cycle-react with other libraries are work in progress.
Meanwhile, See
"Working with React"
for guidelines.
more
docs/faq.md
Build standalone js
npm run dist
- Ask "how do I...?" questions in cycle-react's Gitter:
- Propose and discuss significant changes as a GitHub issues
- In addition, more resources can be found at
Cycle.js' page
Disclaimer
Work in progress
Just like Cycle.js, changes to API will occur before 1.0.
0.24.0
Breaking change: createEventSubject has been removed
Use interactions.getEventSubject(name)
and
interactions.subject(name)
(alias) instead. It's a better design to have
event subscriptions stay within the interactions. See interactions.md
for
details.
Add feature: Disposable element
Sometimes, it's not an easy task to manage all your disposables by
using Observable.using
. This new property of custom-element allows
you to dispose resources when React triggered the componentWillUnmount
lifecycle method.
Cycle.createReactClass('MyElement', () => {
return {
vtree$: ...,
dispose: function dispose() {
// This function will be called during the
// componentWillUnmount lifecycle event
}
// You can set Rx.Disposable objects(e.g. subscription)
// instead of function to the dispose property, too.
}
});
Add feature: Refs compatibility
See working-with-react.md
for details.