L
L
LB7772015-04-15 17:41:19
React
LB777, 2015-04-15 17:41:19

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

3 answer(s)
S
Stepan, 2016-01-12
@xoma2

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>

L
liubko, 2015-04-17
@liubko

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 so
React.findDOMNode(this.refs.myTextInput).focus();

M
Maxim, 2016-03-12
@maxfarseer

Detailed here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question