How to set focus to an input
element when it enters the DOM?
When a button is clicked the input element is displayed. How to set the focu
In the componentDidMount
and componentDidUpdate
hooks do this:
ReactDOM.findDOMNode(this.refs.myInput).focus()
Assuming you only need one visible input on the page at a time to have autofocus Poh Zi How's suggestion of using the autofocus
is probably the simplest.
<input type="text" autofocus/>
should do the trick, no JS needed!