ReactJS: How-to set focus to input-element when it enters the DOM?

后端 未结 2 1519
时光取名叫无心
时光取名叫无心 2021-02-14 19:53

How to set focus to an input element when it enters the DOM?

Scenario

When a button is clicked the input element is displayed. How to set the focu

2条回答
  •  不思量自难忘°
    2021-02-14 20:14

    In the componentDidMount and componentDidUpdate hooks do this:

    ReactDOM.findDOMNode(this.refs.myInput).focus()
    

提交回复
热议问题