Created
October 21, 2017 07:50
-
-
Save princiya/fbf227d6d4f68deaa5e0689351b01093 to your computer and use it in GitHub Desktop.
Reusable React component to render the output of an observable
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 Rx from 'rxjs/Rx'; | |
import './App.css'; | |
import Observable from './components/Observable'; | |
class App extends Component { | |
render() { | |
return ( | |
<div className="App"> | |
<div className="App-header"> | |
<h2>React & Observables</h2> | |
</div> | |
<Observable observable={ | |
Rx.Observable.interval(1000).startWith(-1).map((i) => i + 2) | |
.map((i) => | |
<div> {i} | |
<Observable observable={ | |
Rx.Observable | |
.interval(1000 / (i + 1)).startWith(-1) | |
.map((j) => '-'.repeat(j + 1)) | |
} /> | |
</div> | |
)} | |
/> | |
</div> | |
); | |
} | |
} | |
export default App; |
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'; | |
class Observable extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
x: null | |
}; | |
} | |
componentDidMount() { | |
this.props.observable.subscribe( | |
(x) => { | |
this.setState({ x }); | |
}, | |
e => console.log('onError: %s', e), | |
() => console.log('onCompleted')) | |
} | |
render() { | |
return ( | |
<div> | |
{this.state.x} | |
{this.props.children} | |
</div> | |
); | |
} | |
} | |
export default Observable; |
Author
princiya
commented
Oct 21, 2017
Could you please tell me , If you want to do unsubscribe, how will you do from ComponentWillUnmount? I tried but I see it does not call ComponentWillUnmount. I am not expert, I am learning and working. Also your code is not work anymore . Kindly check that
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment