useDynamicRefs
function useDynamicRefs<T>(options?): DynamicRefGetter<T>;Defined in: use-dynamic-refs.ts:78
React hook that returns a ref “getter” function.
Call the getter with a string key to get (or create) a RefObject for that key.
Useful for dynamic lists, tabs, or any UI where the number of refs isn’t known at compile time.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Description |
|---|---|
T |
The element type for the refs (e.g. HTMLDivElement) |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
options? |
UseDynamicRefsOptions |
Optional configuration |
Returns
Section titled “Returns”A DynamicRefGetter function
Examples
Section titled “Examples”import { useDynamicRefs } from "@xtandard/lib/react";
function TabList({ tabs }: { tabs: string[] }) { const getRef = useDynamicRefs<HTMLButtonElement>();
return ( <div> {tabs.map((tab) => ( <button key={tab} ref={getRef(tab)}> {tab} </button> ))} </div> );}// Scroll to a specific itemconst getRef = useDynamicRefs<HTMLDivElement>();
function scrollTo(id: string) { getRef(id)?.current?.scrollIntoView({ behavior: "smooth" });}