1 | ;
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.default = useRefWithInitialValueFactory;
|
5 |
|
6 | var _react = require("react");
|
7 |
|
8 | var dft = Symbol('default value sigil');
|
9 | /**
|
10 | * Exactly the same as `useRef` except that the initial value is set via a
|
11 | * factroy function. Useful when the default is relatively costly to construct.
|
12 | *
|
13 | * ```ts
|
14 | * const ref = useRefWithInitialValueFactory<ExpensiveValue>(() => constructExpensiveValue())
|
15 | *
|
16 | * ```
|
17 | *
|
18 | * @param initialValueFactory A factory function returning the ref's default value
|
19 | * @category refs
|
20 | */
|
21 |
|
22 | function useRefWithInitialValueFactory(initialValueFactory) {
|
23 | var ref = (0, _react.useRef)(dft);
|
24 |
|
25 | if (ref.current === dft) {
|
26 | ref.current = initialValueFactory();
|
27 | }
|
28 |
|
29 | return ref;
|
30 | } |
\ | No newline at end of file |