Skip to content

Instantly share code, notes, and snippets.

@gbabiars
Created December 20, 2015 21:15
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gbabiars/8f037cc19115815c1a59 to your computer and use it in GitHub Desktop.
Save gbabiars/8f037cc19115815c1a59 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>
)
}
}
@DavidNorena
Copy link

Checkout this It really worked for me !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment