Skip to content

docs(useSyncRef): add jsdoc #7355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/base/src/hooks/useSyncRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@
import type { MutableRefObject, Ref, RefCallback } from 'react';
import { useCallback, useRef } from 'react';

/**
* A hook that synchronizes an external ref (callback or object) with an internal ref.
*
* @example
* ```tsx
* const MyComponent = forwardRef<HTMLDivElement, PropTypes>((props, ref) => {
* const [componentRef, localRef] = useSyncRef<HTMLDivElement>(ref);
*
* useEffect(() => {
* // `localRef.current` is always the latest DOM node (or `null`)
* console.log('current node:', localRef.current);
* }, []);
*
* return <div ref={componentRef}>Hello World!</div>;
* });
* ```
*
* @returns [componentRef, localRef]
* A tuple containing:
* - `componentRef`: a stable callback ref to attach to React elements. When the node
* updates, it will forward the node to the external `ref` and update the internal one.
* - `localRef`: an internal, ref object that holds the latest node for synchronous reads.
*/
export function useSyncRef<RefType = never>(
ref: Ref<RefType>,
): [RefCallback<RefType>, MutableRefObject<RefType | null>] {
Expand Down
Loading