Answer the question
In order to leave comments, you need to log in
How to autofocus input in react.js?
Unfortunately react does not support the autofocus form attribute.
It seems like .focus () allows you to do autofocus, but I can’t figure out how to fasten it.
What are the options for autofocusing a specific input?
Answer the question
In order to leave comments, you need to log in
I did like this
. Everything works 100%. If you need to autofocus after the text that is in the input, I do it like this.
componentDidMount() {
this.refs.searchInput.value = this.refs.searchInput.value
},
<input type="text"
ref="searchInput"
autoFocus={true}
value={this.props.searchTerm}
> </input>
in react v13 it's still possible like this:
https://facebook.github.io/react/docs/more-about-r...
The ref attribute can be a callback function instead of a name. This callback will be executed immediately after the component is mounted. The referenced component will be passed in as a parameter, and the callback function may use the component immediately, or save the reference for future use (or both).
It's as simple as assigning a ref attribute to anything returned from render such as:
or soReact.findDOMNode(this.refs.myTextInput).focus();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question