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>

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.

Further reading

Problem with a question

React · Junior · question 62

Test yourself

Take the React · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz