A custom Preact Hook that provides a sensible alternative to useRef for storing instance variables.
npm install @preact-hooks/instance> Inspired by donovan/use-instance
[![version][version-badge]][package]
[![MIT License][license-badge]][license]
A custom Preact Hook that provides a sensible alternative to useRef for storing instance variables.
- Why?
- Installation
- Docs
- LICENSE
See more in-depth explanation at: donovan/use-instance.
- useRef is weird.
- The fact that you have to access it via .current is strange.
- You treat the object returned by useInstance just like you would this in a class component, so you're already familiar with how it works.
- Use useRef if you're dealing with actual DOM elements.
- Use useInstance for instance properties and methods.
This package has Preact 10+ as a peer dependency.
npm install @preact-hooks/instance or yarn add @preact-hooks/instance
You can also load it via the unpkg CDN
https://unpkg.com/@preact-hooks/instance will download the latest UMD bundle.
For usage and examples see donovan/use-instance.
[package]: https://www.npmjs.com/package/@preact-hooks/instance
[version-badge]: https://img.shields.io/npm/v/@preact-hooks/instance
[license]: https://github.com/mihar-22/preact-hooks-instance/blob/master/LICENSE
[license-badge]: https://img.shields.io/github/license/mihar-22/preact-hooks-instance?color=b