简体   繁体   中英

React.js Ref focus()

I have a boolean in my component's state that I am using to decide whether an element should be in focus.

I use React.createRef() in the constructor and assign the ref to the <input> through its props. Then in render, I use an if statement to check whether the boolean is true, and call inputRef.current.focus() to bring it into focus if it is. The if statement is being triggered, but the element simply isn't being brought into focus.

Any help would be greatly appreciated!

you should use this.inputRef.current.focus()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM