@vtaits/use-lazy-ref
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

NPM

@vtaits/use-lazy-ref

React.useRef with initialization callback

Example

Good

import { useLazyRef } from '@vtaits/use-lazy-ref';

const ref = useLazyRef(() => getRefInitialValue());
  1. No extra boilerplate.
  2. Inferred type of ref.
  3. Typescript knows that a value of ref cannot be undefined (if it's not a possible result of getRefInitialValue).

Bad

import { useRef } from 'react';

const ref = useRef<ValueType>();

if (!ref.current) {
  ref.current = getRefInitialValue();
}
  1. Extra boilerplate.
  2. Ref type must be set.
  3. Typescript thinks value of ref can be undefined.

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.1.3
    43,455
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.1.3
    43,455
  • 0.1.1
    528
  • 0.1.0
    24,314

Package Sidebar

Install

npm i @vtaits/use-lazy-ref

Weekly Downloads

68,297

Version

0.1.3

License

MIT

Unpacked Size

6.74 kB

Total Files

9

Last publish

Collaborators

  • vtaits