UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

21 lines (17 loc) 714 B
import { useCallback, useEffect, useRef } from "react"; /** * A custom hook that converts a callback to a ref to avoid triggering re-renders when passed as a * prop or avoid re-executing effects when passed as a dependency * https://github.com/radix-ui/primitives/blob/main/packages/react/use-callback-ref/src/useCallbackRef.tsx */ export function useCallbackRef<T extends (...args: any[]) => any>( callback: T | undefined, deps: React.DependencyList = [], ) { const callbackRef = useRef(callback); useEffect(() => { callbackRef.current = callback; }); // eslint-disable-next-line react-hooks/exhaustive-deps return useCallback(((...args) => callbackRef.current?.(...args)) as T, deps); }