I have this simple form on React js
render: function(){
return (
<div>
<form onSubmit={this.hanldeSubmit} >
<input
type='text' placeholder='What do you need to do?' required
onChange={this.handleChange} value={this.state.value}
/>
<button className='button expanded'>Add Todo</button>
</form>
</div>
);
}
Here is the on submit handler. I want to set focus on input field. Please tell me the more right way doing this in controlled component WITHOUT refs:
hanldeSubmit: function(e){
e.preventDefault();
var {value} = this.state;
if(value){
this.props.onAddTodo(value);
this.setState({value: ''});
}
//how to set focus on input field?
},
I found this code with refs:
this.refs.todoText.focus()
Thanks in advance
UPDATE:
Ok if I use refs to set focus:
this.refs.yourInputBox.focus();
Should I keep my component being 'controlled' ? What is the 'best practice'?