Created
June 19, 2018 00:23
-
-
Save juliankrispel/e753a5fc3c6b940897d6ac9c6dd2dbc4 to your computer and use it in GitHub Desktop.
Extremely basic way of rendering observables as components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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