Why "current" in inputRef.current.focus()? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why "current" in inputRef.current.focus()?

Using useRef I create a reference to an input element named inputRef. To set focus on the element I call: inputRef.current.focus() What does "current" mean and why is it needed? Why not just: inputRef.focus() ?

8th Jun 2022, 10:31 PM
Adam Aksu
Adam Aksu - avatar
1 Answer
+ 1
useRef return an object that can hold a mutable value in its .current property. If you pass a ref object to React with <div ref={myRef} />, React will set its .current property to the corresponding DOM node whenever that node changes.
9th Jun 2022, 6:31 AM
Calviղ
Calviղ - avatar