Skip to content

docs: add callback ref example for focusing input on mount (useRef page) - #8766

Open
pawannyadav05 wants to merge 1 commit into
reactjs:mainfrom
pawannyadav05:docs/add-callback-ref-focus-example
Open

pawannyadav05 wants to merge 1 commit into
reactjs:mainfrom
pawannyadav05:docs/add-callback-ref-focus-example

Conversation

@pawannyadav05

@pawannyadav05 pawannyadav05 commented Nov 20, 2026 •

Copy link
Copy Markdown

Closes #8718

Summary

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*/}.

Adds a new recipe to the useRef reference page demonstrating how to
focus an input element on mount using a callback ref (function ref)
rather than a useRef object ref.

This addresses the documented use case in issue reactjs#8718 where users
want to focus an element automatically without needing a button click,
and where useCallback ensures the ref callback is stable across renders.

Closes reactjs#8718
@github-actions

Copy link
Copy Markdown

Size changes

Details

📦 Next.js Bundle Analysis for react-dev

This analysis was generated by the Next.js Bundle Analysis action. 🤖

This PR introduced no changes to the JavaScript bundle! 🙌

This branch was successfully deployed

1 active deployment
Preview — 44f2a001 Deployed Nov 20, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Suggestion]: Add example of focusing an input using callback refs

1 participant