Last active
August 29, 2015 14:22
-
-
Save cqfd/3a05c79f53dda47d0dcc to your computer and use it in GitHub Desktop.
Example of using co and React.
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
const Promise = require('bluebird'); | |
const co = require('co'); | |
const React = require('react'); | |
function* sleep(ms) { | |
yield Promise.delay(ms); | |
} | |
function usersView(users) { | |
const lis = users.map(u => <li>{u.name}</li>); | |
return <ul>{lis}</ul>; | |
} | |
const UsersComponent = React.createClass({ | |
getInitialState() { | |
return { users: [] } | |
}, | |
componentDidMount() { | |
co(function*() { | |
while (true) { | |
const users = yield $.ajax("/home/api"); | |
this.setState({users: users}); | |
yield sleep(2000); | |
} | |
}.bind(this)).catch(e => console.log("hmm", e)); | |
}, | |
render() { | |
return usersView(this.state.users); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment