GoScrobble/web/node_modules/@restart/hooks/esm/useRefWithInitialValueFactory.js

24 lines
653 B
JavaScript
Raw Normal View History

2022-04-25 02:47:15 +00:00
import { useRef } from 'react';
var dft = Symbol('default value sigil');
/**
* Exactly the same as `useRef` except that the initial value is set via a
* factroy function. Useful when the default is relatively costly to construct.
*
* ```ts
* const ref = useRefWithInitialValueFactory<ExpensiveValue>(() => constructExpensiveValue())
*
* ```
*
* @param initialValueFactory A factory function returning the ref's default value
* @category refs
*/
export default function useRefWithInitialValueFactory(initialValueFactory) {
var ref = useRef(dft);
if (ref.current === dft) {
ref.current = initialValueFactory();
}
return ref;
}