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.

Revisions

  1. @gbabiars gbabiars created this gist Dec 20, 2015.
    43 changes: 43 additions & 0 deletions Scores.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    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>
    )
    }
    }