@primer/react
Version:
An implementation of GitHub's Primer Design System using React
26 lines (25 loc) • 1.21 kB
TypeScript
import React from "react";
//#region src/hooks/useProvidedRefOrCreate.d.ts
/**
* There are some situations where we only want to create a new ref if one is not provided to a component
* or hook as a prop. However, due to the `rules-of-hooks`, we cannot conditionally make a call to `React.useRef`
* only in the situations where the ref is not provided as a prop.
* This hook aims to encapsulate that logic, so the consumer doesn't need to be concerned with violating `rules-of-hooks`.
* @param providedRef The ref to use - if undefined, will use the ref from a call to React.useRef
* @type TRef The type of the RefObject which should be created.
*
* @deprecated This hook is incompatible with forwarded callback refs. Prefer `useMergedRefs` with an internally
* created ref instead.
*
* ```diff
* - const ref = useProvidedRefOrCreate(forwardedRef as RefObject<...>)
* + const ref = useRef(null)
* + const mergedRef = useMergedRefs(forwardedRef, ref)
*
* - return <div ref={ref} />
* + return <div ref={mergedRef} />
* ```
*/
declare function useProvidedRefOrCreate<TRef>(providedRef?: React.RefObject<TRef | null>): React.RefObject<TRef | null>;
//#endregion
export { useProvidedRefOrCreate };