Created
November 26, 2015 18:22
-
-
Save sabcio/b42ab2831fa0b32039e2 to your computer and use it in GitHub Desktop.
react this.refs is {}
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
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