Subscribe to an Observable declaratively with the <Subscribe>{observable}</Subscribe>
component.
npm install --save react-observable-subscribe
This library's default export is a Subscribe
component which you can use in your JSX to declaratively subscribe and render Observable streams. Just pass the observable as the only child to the the component.
You can apply any operators you want to your observable before passing it to <Subscribe>
--you don't have to use RxJS, the only requirement is the the observable supports observable[Symbol.observable]()
from the TC39 spec. This library doesn't come with any actual Observable implementation itself.
import Subscribe from 'react-observable-subscribe';
// ...other imports
class Example extends Component {
render() {
return (
<div>
<div>
<h3>Every 100ms:</h3>
<div>
<Subscribe>{this.props.stream}</Subscribe>
</div>
</div>
<div>
<h3>Every 1s:</h3>
<div>
<Subscribe>{this.props.stream.throttleTime(1000)}</Subscribe>
</div>
</div>
<div>
<h3>Every 100ms w/ <input> element: </h3>
<div>
<Subscribe>
{this.props.stream.map(
value => <input value={value} readOnly />
)}
</Subscribe>
</div>
</div>
</div>
);
}
}
// This Observable will emit an incrementing
// number every 100ms
let stream = Observable.interval(100);
ReactDOM.render(<Example stream={stream} />, container);
The observable can emit simple primitives (e.g. strings, numbers) or you can even emit JSX elements! Each "onNext" just needs to be a single value, arrays are not supported because of React limitations.
When the observable emits new values, only the content inside <Subscribe>
will re-render, not the component in which you declare it, so it's very efficient.
Depending on your preferences, you might find it helpful to use a shorter name for Subscribe
when you import it. Since it's the default export, what you name it is totally up to you:
import Sub from 'react-observable-subscribe';
// etc
<div>
<Sub>{stream.throttleTime(1000)}</Sub>
</div>
react-observable-subscribe's People
Forkers
rubythonodeRecommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.