How to focus an input element on page load?

  • Use the autoFocus attribute on the input element
  • Use the focus() method in componentDidMount()
  • Use the onFocus() event in the render method
  • Use the onLoad() event on the body element
In React, you can focus an input element on page load by using the "focus()" method in the "componentDidMount()" lifecycle method. This will set the focus to the input element after the component has been mounted in the DOM. For example: componentDidMount() { this.myInput.focus(); }.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *