UNPKG

269 BTypeScriptView Raw
1import * as React from 'react';
2
3export default function useLazyRef<T>(callback: () => T) {
4 const lazyRef = React.useRef<T | undefined>();
5
6 if (lazyRef.current === undefined) {
7 lazyRef.current = callback();
8 }
9
10 return lazyRef as React.MutableRefObject<T>;
11}