React · Junior · Question 62 of 75
How do you focus an input when a button is clicked?
const inputRef = useRef(null);
// <input ref={inputRef} />
// <button onClick={() => inputRef.current.focus()}>Edit</button>- ✓Correct answer: Attach a ref with
useRefand callinputRef.current.focus() - 2Use
document.querySelectorinside render - 3Set
focus={true}as a prop
Explanation
React fills ref.current with the DOM node after mounting. Use refs for imperative actions like focus, scroll and measuring - not to change content.