Skip to content

Instantly share code, notes, and snippets.

@tommymarshall
Last active August 29, 2015 14:21
Show Gist options
  • Save tommymarshall/16da15ea29dfc44aaac4 to your computer and use it in GitHub Desktop.
Save tommymarshall/16da15ea29dfc44aaac4 to your computer and use it in GitHub Desktop.
simple ajax fill options
var React = require('react')
var request = require('superagent')
module.exports = React.createClass({
getInitialState: function() {
return {
ready : false,
options : [],
}
},
componentDidMount: function() {
this.getOptions()
},
getOptions: function() {
request
.get('/route')
.set('Accept', 'application/json')
.end(function(error, res){
if (error) return false
this.setState({
ready : true,
options : JSON.parse(res.text)
})
}.bind(this))
},
renderOptions: function(option) {
return <option>{ option }</option>
}
render: function() {
if (!this.state.ready) {
return (
<div>
Loading...
</div>
)
}
return (
<select>
{this.options.map(this.renderOptions)}
</select>
)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment