@vtaits/use-lazy-ref
v0.1.3
Published
[![NPM](https://img.shields.io/npm/v/@vtaits/use-lazy-ref.svg)](https://www.npmjs.com/package/@vtaits/use-lazy-ref)
Downloads
355,638
Maintainers
Readme
@vtaits/use-lazy-ref
React.useRef
with initialization callback
Example
Good
import { useLazyRef } from '@vtaits/use-lazy-ref';
const ref = useLazyRef(() => getRefInitialValue());
- No extra boilerplate.
- Inferred type of ref.
- Typescript knows that a value of ref cannot be
undefined
(if it's not a possible result ofgetRefInitialValue
).
Bad
import { useRef } from 'react';
const ref = useRef<ValueType>();
if (!ref.current) {
ref.current = getRefInitialValue();
}
- Extra boilerplate.
- Ref type must be set.
- Typescript thinks value of ref can be
undefined
.