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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ptrs

v0.0.3

Published

Manage your state with pointers.

Readme

ptrs

Manage your state with pointers.

npm i ptrs

Basic concepts

React state handling and data flow can be tedious. You have to think about immutability and how to pass data around to other components. This can be even trickier when you have to deal with hooks. As a consequence, a component or hook could update too much or too little. This can be solved by using pointers. Pointers themselves are immutable, but they mutate their values in a React-compatible way.

import { createPointer, useStatePointer } from "ptrs"

// general purpose pointer, like for a global state
const pointer = createPointer("foo")

// React hook for a state pointer
const pointer = useStatePointer("foo")

pointer() // access the value
pointer("bar") // change the value

// also works for properties
const pointer = useStatePointer({foo: {bar: "foo"}})
pointer.foo.bar()
pointer.foo.bar("bar")

// simple prop usage
<Foobar foo={pointer.foo} bar={pointer.foo.bar} />

Usage in components

Because pointers are immutable, React doesn't know when to re-render a component. There are two preferred methods to use a pointer as render input.

const globalBar: Pointer<string>

// usePointer hook.
const Foobar = (props: {bar: Pointer<string>}) => {
    const { bar } = props
    usePointer(bar, globalBar) // re-render when any of these change
    return <>{bar()} and {globalBar()}</>
}

// "ptrs" component wrapper.
const Foobar = ptrs((props: {bar: Pointer<string>}) => {
    return <>{bar()} and {globalBar()}</> // just read from a pointer
})

Usage of pointers in hooks, like useEffect or useCallback, is simple. Just use them. If a pointer value is only used inside of a hook, usePointer or ptrs is not needed.

// technically name is not needed as a dependency, but makes linters happy :)
const onChangeName = useCallback((newValue) => {
    if (name() !== newValue) {
        name(newValue)
    }
}, [name]) 

Working with array pointers

Pointers can also be used to manage arrays and their elements. You can create a pointer to an array, and then use property pointers to access or update individual items or the array as a whole.

// Create a pointer to an array
const items = useStatePointer(["foo", "bar"])

// Read and update like any other pointer
items()
items(["bar", "foo"])

// Index access will result in a pointer to that index
const firstItem = items[0]
firstItem()
firstItem("new value")

// All Array methods and length work as usual.
// Mutating methods trigger a pointer update.
items.length // 2
items.push("baz")
items.length // 3

Working with object methods

Pointers to objects support direct method calls for mutation, just like you would use methods on a regular object. When they mutate this or any descendant properties, the object pointer will update.

const state = useStatePointer({ 
    amount: 0,

    increment() {
        this.amount++
    }

    dispatch(type: string, value: any) {
        if (type === "amount") {
            this.amount = value
        }
    }
})

state.increment()
state.amount() // 1
state.dispatch("amount", 2)
state.amount() // 2

Customizing pointer behavior with pointerSchema

You can use pointerSchema to define how specific properties or methods of an object should behave when used as a pointer. This is useful for advanced scenarios where you want to control whether a property acts as a pointer, a readonly getter or function, a mutating function, or a get-set property. The default of normal properties is to act as a pointer and for methods to act as a mutating function.

import { pointerSchema, createPointer } from "ptrs"

// Mark a method as readonly (does not mutate state)
const state = createPointer(pointerSchema(
    {
        count: 0,
        getCount() {
            return this.count
        }
    },
    { getCount: "readonly" }
))

state.getCount() // 0

// Mark a property as get-set
const state2 = createPointer(pointerSchema(
    {
        _value: 1,
        get value() { return this._value },
        set value(v) { this._value = v }
    }, 
    { value: "get-set" }
))

state2.value // 1
state2.value = 2
state2.value // 2

Property types you can specify:

  • "pointer": Treat as a pointer (default for non-methods)
  • "readonly": Method or getter that does not mutate state
  • "mutate": Method or getter that mutates state
  • "get-set": Property with getter and setter