useRef returns a mutable ref object whose .current property is initialized to the passed argument (initialValue). The returned object will persist for the full lifetime of the component.
🚀 Usage
Pretty
Copy
1
functionDemo(){
constinputEl=useRef(null);
constonButtonClick=()=>{
// `current` points to the mounted text input element
inputEl.current.focus();
};
return(
<>
<inputref={inputEl}type="text"/>
<buttononClick={onButtonClick}>Focus the input</button>