npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pailhead/use-init-ref

v0.1.4

Published

useRef for react but it doesnt create/initialize the contents with ever render

Downloads

1

Readme

use-init-ref

npm version

Demo

Codesandbox

Usage

Be explicit that useInitRef takes an initializer, use normal useRef for primitives.

import {useInitRef} from '@pailhead/use-init-ref'
const myRef = useInitRef(()=>new Matrix4())

Or, if you want to just swap out useRef altogether, you can overload it like so:

import {useRef} from '@pailhead/use-init-ref'
const myRef = useRef(null,()=>new Matrix4())

myRef gets inferred as MutableRefObject<Matrix4>

Issues

The most concise and easiest way to get what you want is to express it like so:

const myRef = useRef(new Matrix4())

But this will create a new instance of Matrix4() every time the component renders. It's not a memory leak, as these will eventually be garbage collected. The docs say to do this:

const myRef = useRef<?>(null)
if(myRef.current===null){
  myRef.current = new Matrix4()
}

But for TS users, what do you declare as a type?

  • Just passing null will infer it as MutableRefObject<null>
  • Typing it as Matrix4() requires you to cast null as something
  • Typing it as Matrix4|null changes the whole program

In general, TS or not, if you want to later nullify this reference, ie do:

myRef.current = null

It seems impossible, since the render loop would just instantiate it again.

Some suggested:

const [myMatrix] = useState(()=>new Matrix4())

But you don't get a MutableRefObject at all, there is no current to be changed.