Skip to content

Instantly share code, notes, and snippets.

@juliankrispel
Created June 19, 2018 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliankrispel/e753a5fc3c6b940897d6ac9c6dd2dbc4 to your computer and use it in GitHub Desktop.
Save juliankrispel/e753a5fc3c6b940897d6ac9c6dd2dbc4 to your computer and use it in GitHub Desktop.
Extremely basic way of rendering observables as components
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import { interval } from 'rxjs'
class RenderObservable extends Component {
state = {
value: null
}
mountObservable() {
if (!this._subscription && this.props.observable != null) {
this._subscription = this.props
.observable
.subscribe(value => this.setState({ value }))
}
}
componentDidMount() {
this.mountObservable()
}
componentDidUpdate() {
this.mountObservable()
}
componentWillUnmount() {
this._subscription.unsubscribe()
}
render() {
return this.state.value
}
}
class App extends Component {
_obs$ = interval(1000)
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<RenderObservable observable={this._obs$} />
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
</div>
);
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment