+1 vote
in ReactJS by

How to focus an input element on page load?

1 Answer

0 votes
by

You can do it by creating ref for input element and using it in componentDidMount():

class App extends React.Component{
  componentDidMount() {
    this.nameInput.focus()
  }

  render() {
    return (
      <div>
        <input
          defaultValue={'Won\'t focus'}
        />
        <input
          ref={(input) => this.nameInput = input}
          defaultValue={'Will focus'}
        />
      </div>
    )
  }
}

ReactDOM.render(<App />, document.getElementById('app'))

Related questions

0 votes
asked Jan 31, 2020 in Selenium by SakshiSharma
0 votes
0 votes
asked Aug 19, 2019 in Selenium by rahulsharma
...