in React JS by
Q:

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'))
Click here to read more about React JS
Click here to read more about Insurance

Related questions

0 votes
0 votes
asked Feb 15, 2020 in Angular by matthenary
0 votes
asked Apr 28, 2020 in Internet of Things IoT by SakshiSharma
0 votes
+2 votes
asked Mar 4, 2020 in Agile by emanuela.scavizzi
0 votes
asked Mar 1, 2020 in React JS by RShastri
0 votes
asked Jan 12, 2020 in Big Data | Hadoop by sharadyadav1986
0 votes
...