Repository navigation
docs: add callback ref example for focusing input on mount (useRef page) - #8766
Open
pawannyadav05 wants to merge 1 commit into
Open
pawannyadav05 wants to merge 1 commit into
pawannyadav05 wants to merge 1 commit into
Conversation
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
Size changesDetails📦 Next.js Bundle Analysis for react-devThis analysis was generated by the Next.js Bundle Analysis action. 🤖 This PR introduced no changes to the JavaScript bundle! 🙌 |
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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*/}.