Created
December 20, 2015 21:15
-
-
Save gbabiars/8f037cc19115815c1a59 to your computer and use it in GitHub Desktop.
Basic example of RxJS and React
This file contains hidden or 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 from 'react'; | |
import axios from 'axios'; | |
import Rx from 'rxjs'; | |
export default class App extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
scores: [] | |
}; | |
} | |
componentWillMount() { | |
Rx.Observable.interval(30000).merge(Rx.Observable.of(0)) | |
.mergeMap(() => { | |
return Rx.Observable.fromPromise(axios.get('/api/scores')); | |
}) | |
.map(result => { | |
return result.data; | |
}) | |
.subscribe(scores => { | |
this.setState({ scores: scores }); | |
}); | |
} | |
render() { | |
let listItems = this.state.scores.map(score => { | |
return ( | |
<li key={score.id}> | |
{`${score.home.city} ${score.home.points} - ${score.visitor.city} ${score.visitor.points}`} | |
</li> | |
); | |
}); | |
return ( | |
<div> | |
<h3>Scores:</h3> | |
<ul> | |
{listItems} | |
</ul> | |
</div> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Checkout this It really worked for me !