Skip to content

Instantly share code, notes, and snippets.

@TimCodes
Forked from gbabiars/Scores.js
Created May 10, 2017 21:59
Show Gist options
  • Save TimCodes/09d2bc2a01c0bb234886d74d3bce556a to your computer and use it in GitHub Desktop.
Save TimCodes/09d2bc2a01c0bb234886d74d3bce556a to your computer and use it in GitHub Desktop.
Basic example of RxJS and React
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