Skip to content

Instantly share code, notes, and snippets.

@sabcio
Created November 26, 2015 18:22
Show Gist options
  • Save sabcio/b42ab2831fa0b32039e2 to your computer and use it in GitHub Desktop.
Save sabcio/b42ab2831fa0b32039e2 to your computer and use it in GitHub Desktop.
react this.refs is {}
class MashForm extends React.Component{
constructor(props){
super(props)
this.state = {malts: []}
}
componentDidMount() {
this._fetchIngredients()
}
render(){
return(
<div>
<form onSubmit={this.props.addHandler} className="form-inline" >
<Select
options={this.state.malts}
ref='malt'
onChange={this._onChange} />
<Input
type="text"
placeholder="Amount"
ref='amount'
onChange={this._onChange} />
<Input
type="text"
placeholder="Time"
ref='time'
onChange={this._onChange} />
<Button type="submit">Add</Button>
</form>
</div>
);
}
_onChange(){
console.log(this.refs)
let malt = ReactDOM.findDOMNode(this.refs.malt).value;
let amount = ReactDOM.findDOMNode(this.refs.amount).value;
let time = ReactDOM.findDOMNode(this.refs.time).value;
console.log('onChange')
this.props.handleChange({malt: malt, amount: amount, time: time})
}
_fetchIngredients() {
$.ajax({
url: this.props.url,
dataType: 'json',
cache: false,
success: function(data) {
this.setState({malts: data});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
}
};
MashForm.defaultProps = {url: '/api/malts'};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment