UNPKG

@workday/canvas-kit-react

Version:

The parent module that contains all Workday Canvas Kit React components

19 lines (15 loc) 406 B
import {useRef} from 'react'; /** * Get a constant value in React, only running the callback function once. `React.useMemo` does not * guarantee that a function will only be run once. * * @example * const value = useConstant(generateUniqueId) */ export function useConstant<T>(fn: () => T): T { const ref = useRef<T>(); if (!ref.current) { ref.current = fn(); } return ref.current; }