Inspired by donovan/use-instance
A custom Preact Hook that provides a sensible alternative to useRef for storing instance variables.
See more in-depth explanation at: donovan/use-instance.
useRefis weird.- The fact that you have to access it via
.currentis strange. - You treat the object returned by
useInstancejust like you wouldthisin a class component, so you're already familiar with how it works. - Use
useRefif you're dealing with actual DOM elements. - Use
useInstancefor 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.