You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Suggestion]: Add example of focusing an input using callback refs #8718
React calls the callback ref when the DOM element is attached.
The DOM element is passed as an argument to the callback.
We can directly call DOM methods such as .focus() inside the callback.
React calls the callback with null when the element is detached (for the usual callback-ref pattern).
It would be helpful to include this approach alongside the useRef and useEffect example to demonstrate how callback refs can be used for DOM interactions.
Page
No response
Details
Add documentation explaining how callback refs can be used to access DOM elements and perform actions such as automatically focusing an input when it mounts, with an example using an arrow function.
I searched the docs first. Ref callbacks are already covered in "Manipulating the DOM with Refs", including the list-of-refs Deep Dive, and the built-in autoFocus prop is documented on the <input>, <select> and <textarea> reference pages. The "Synchronizing with Effects" page also has a challenge about focusing an input on mount with an Effect. I couldn't find an example that shows a ref callback used to focus an element when it mounts.
I'd like to keep the change small:
Add a short example to "Manipulating the DOM with Refs" that focuses an input on mount using a ref callback, with one or two sentences of explanation. I'd note that autoFocus is the simpler choice for plain autofocus, so the example is about how ref callbacks receive the DOM node, not a recommendation to replace autoFocus.
Use a stable callback (defined outside the component) rather than an inline arrow function. An inline function is a new function on every render, so React would call it again after each re-render, which could re-focus the input unexpectedly.
Optionally, if it fits, add a similar example to the ref callback section of the API reference.
Is this something you'd want in the docs, or would you prefer a different scope? If it sounds good, I'll open a PR. Happy to adjust based on your feedback.
On Fri, 30 Oct 2026, 15:00 krishnayak986-lang, ***@***.***> wrote:
*krishnayak986-lang* left a comment (reactjs/react.dev#8718)
<#8718 (comment)>
Hi! I'd like to work on this issue.
I searched the docs first. Ref callbacks are already covered in
"Manipulating the DOM with Refs", including the list-of-refs Deep Dive, and
the built-in autoFocus prop is documented on the <input>, <select> and
<textarea> reference pages. The "Synchronizing with Effects" page also
has a challenge about focusing an input on mount with an Effect. I couldn't
find an example that shows a ref callback used to focus an element when it
mounts.
I'd like to keep the change small:
1. Add a short example to "Manipulating the DOM with Refs" that
focuses an input on mount using a ref callback, with one or two sentences
of explanation. I'd note that autoFocus is the simpler choice for
plain autofocus, so the example is about how ref callbacks receive the DOM
node, not a recommendation to replace autoFocus.
2. Use a stable callback (defined outside the component) rather than
an inline arrow function. An inline function is a new function on every
render, so React would call it again after each re-render, which could
re-focus the input unexpectedly.
3. Optionally, if it fits, add a similar example to the ref callback
section of the API reference.
Is this something you'd want in the docs, or would you prefer a different
scope? If it sounds good, I'll open a PR. Happy to adjust based on your
feedback.
—
Reply to this email directly, view it on GitHub
<#8718?email_source=notifications&email_token=A3RARKMPJP7TGNGAYK5UD7D5WRNSFA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTMNJRHE2TANRZGIYKM4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KYZTPN52GK4S7MNWGSY3L#issuecomment-6519506920>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A3RARKLNUXKOAAJBALYPNW35WRNSFAVCNFSNUABFKJSXA33TNF2G64TZHMYTANJZGYZTENJTHNEXG43VMU5TMMJRGQ3TKNBRHAZ2C5QC>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
Thanks for working on this! The example is clear and the placement right after the existing focus example makes sense.
A few small suggestions, feel free to ignore any that don't fit:
The other examples on this page are interactive Sandpack examples, so it might be worth making this one a Sandpack too, for consistency.
It could help to briefly explain why the callback should be stable: an inline function is a new function on every render, so React would call it again after each re-render and refocus the input.
The autoFocus link points to MDN. It may be better to link to the react.dev reference page (/reference/react-dom/components/input) instead.
Hi! I've opened PR #8766 adding an interactive Sandpack recipe showing how to auto-focus an input on mount using a callback ref with useCallback on the useRef reference page.
Adds an interactive Sandpack recipe to the useRef reference page demonstrating how to focus an input element on mount using a callback ref (useCallback) rather than a useRef object ref.
Problem
The useRef API reference page exclusively demonstrated manipulating DOM elements using ref objects (useRef(null) + button click handler).
However, developers frequently encounter scenarios where an input element needs to be focused automatically on mount. Relying solely on ref objects often leads developers to introduce an unnecessary useEffect hook just to call .focus() after mounting.
Furthermore, callback refs are an essential React primitive with distinct lifecycle behavior (called with the DOM node on mount and null on unmount) that was not illustrated with a runnable code example on the reference page.
Solution
Interactive Sandpack Recipe: Added a standalone #### Focusing an input using a callback ref recipe directly following the existing "Focusing a text input" section.
Idiomatic React Pattern: Demonstrates using useCallback with an empty dependency array to ensure a stable ref callback across renders, along with an explicit null-check (if (node !== null)) to safely handle node attachment and cleanup.
Clear Explanation: Explains React's callback ref lifecycle contract and when to choose this pattern over standard ref objects.
Changes
src/content/reference/react/useRef.md: Added recipe section with interactive Sandpack component and localized anchor {/*focusing-an-input-using-a-callback-ref*/}.
Activity
krishnayak986-lang commentedon Oct 30, 2026
Hi! I'd like to work on this issue.
I searched the docs first. Ref callbacks are already covered in "Manipulating the DOM with Refs", including the list-of-refs Deep Dive, and the built-in
autoFocusprop is documented on the<input>,<select>and<textarea>reference pages. The "Synchronizing with Effects" page also has a challenge about focusing an input on mount with an Effect. I couldn't find an example that shows a ref callback used to focus an element when it mounts.I'd like to keep the change small:
autoFocusis the simpler choice for plain autofocus, so the example is about how ref callbacks receive the DOM node, not a recommendation to replaceautoFocus.refcallback section of the API reference.Is this something you'd want in the docs, or would you prefer a different scope? If it sounds good, I'll open a PR. Happy to adjust based on your feedback.
Ojasvdixit commentedon Nov 4, 2026
krishnayak986-lang commentedon Nov 12, 2026
Thanks for working on this! The example is clear and the placement right after the existing focus example makes sense.
A few small suggestions, feel free to ignore any that don't fit:
autoFocuslink points to MDN. It may be better to link to the react.dev reference page (/reference/react-dom/components/input) instead.Thanks again!
pawannyadav05 commentedon Nov 20, 2026
Hi! I've opened PR #8766 adding an interactive Sandpack recipe showing how to auto-focus an input on mount using a callback ref with
useCallbackon theuseRefreference page.pawannyadav05 commentedon Nov 20, 2026
Closes #8718
Summary
Adds an interactive Sandpack recipe to the
useRefreference page demonstrating how to focus an input element on mount using a callback ref (useCallback) rather than auseRefobject ref.Problem
The
useRefAPI reference page exclusively demonstrated manipulating DOM elements using ref objects (useRef(null)+ button click handler).However, developers frequently encounter scenarios where an input element needs to be focused automatically on mount. Relying solely on ref objects often leads developers to introduce an unnecessary
useEffecthook just to call.focus()after mounting.Furthermore, callback refs are an essential React primitive with distinct lifecycle behavior (called with the DOM node on mount and
nullon unmount) that was not illustrated with a runnable code example on the reference page.Solution
#### Focusing an input using a callback refrecipe directly following the existing "Focusing a text input" section.useCallbackwith an empty dependency array to ensure a stable ref callback across renders, along with an explicit null-check (if (node !== null)) to safely handle node attachment and cleanup.Changes
src/content/reference/react/useRef.md: Added recipe section with interactive Sandpack component and localized anchor{/*focusing-an-input-using-a-callback-ref*/}.PR raised in #8766.