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

@isograph/react-disposable-state

v0.5.6

Published

Primitives for managing disposable state in React

Readme

@isograph/react-disposable-state

Primitives for managing disposable items in React state.

This library's purpose is to enable safely storing disposable items in React state. These hooks seek to guarantee that each disposable item is eventually destroyed when it is no longer used and that no disposable item is returned from a library hook after it has been disposed.

This library's goals do not include being ergonomic. A library built on top of react-disposable-state should expose easier-to-use hooks for common cases. Application developers can use the hooks exposed in react-disposable-state when more complicated cases arise.

This is unstable, alpha software. The API is likely to change.

Conceptual overview

What is a disposable item?

A disposable item is anything that is either explicitly created or must be explicitly cleaned up. That is, it is an item with a lifecycle.

A disposable item is safe to use as long as its destructor has not been called.

Code that manages disposable items (such as the useDisposableState hook) should also ensure that each destructor is eventually called, and should not provide access to the underlying item once the destructor has been called.

Disposable items are allowed to have side effects when created or when destroyed.

What is disposable state?

Disposable state is React state that contains a disposable item.

Examples of disposable items

  • A subscription that periodically updates a displayed stock price. When the component where the stock price is displayed is unmounted, the subscription should be disposed, so as to avoid doing unproductive work.
  • References to items that are stored externally. For example, consider a centralized store of profile photos. Photos are stored centrally to ensure consistency, meaning that every component displaying a given profile photo displays the same photo. In order to avoid the situation where no profile photo is ever garbage collected, individual components' "claims" to profile photos must be explicitly created and disposed.
  • Items which you want to create exactly once when a functional React component is first rendered, such as a network request.
    • Due to how React behaves, this state must be stored externally. Hence, this can be thought of as an example of the previous bullet point.
    • Other frameworks make different choices. For example, a SolidJS component function is called exactly once. In these cases, the network request can easily be executed once, without being stored in external state.

How does disposable state differ from regular React state?

Disposable state stands in contrast to "regular" React state (e.g. if {isVisible: boolean, currentlySelectedItem: Item} was stored in state), where

  • creating the JavaScript object is the only work done when creating the regular state, and therefore it is okay to create the state multiple times; and
  • the only necessary cleanup work is garbage collection of the underlying memory.

In particular, it is unobservable to the outside world if a piece of "regular" state is created multiple times.

Can React primitives handle disposable state?

The primitives provided by React are a poor fit for storing disposable items in state. An upcoming blog post will explore this in more detail.

This library

Guarantees

This library guarantees that:

  • First, each disposable item that is created is eventually disposed.

    React and suspense prevent this library from ensuring that each disposable item is disposed immediately when the hook unmounts. Instead, the best we can do if a component suspends is often dispose after a configurable timeout.

  • Second, no disposable item is returned from a library hook after it has been disposed.

  • Third, if a component has committed, no disposable item returned from a library hook will be disposed while it is accessible from a mounted component.

    Colloquially, this means that disposable items returned from library hooks are safe to use in event callbacks.

    This guarantee is not upheld if an item returned from a library hook is re-stored in another state hook. So, don't do that!

Supported behaviors

The hooks in this library enable the following behavior:

  • Lazily creating a disposable item. In this context, "lazily" means creating the item during the render phase of a component, before that component has committed. The item is then available in the functional component.

    Note that this is how Relay uses the term lazy. Libraries like react-query use the word lazy differently.

  • Creating a disposable item outside of the render phase and after a hook's initial commit and storing the item in React state, making it available during the next render of that functional component.

API Overview

useLazyDisposableState

A hook that:

  • Takes a mutable parent cache and a loader function, and returns a { state: T }.
  • The returned T is guaranteed to not be disposed during the tick of the render.
  • If this hook commits, the returned T will not be disposed until the component unmounts.
const { state }: { state: T } = useLazyDisposableState<T>(
  parentCache,
  factory,
  options,
);

useUpdatableDisposableState

A hook that:

  • Returns a { state, setState } object.
  • setState throws if called before the initial commit.
  • The state (a disposable item) is guaranteed to be undisposed during the tick in which it is returned from the hook. It will not be disposed until after it can no longer be returned from this hook, even in the presence of concurrent rendering.
  • Every time the hook commits, a given disposable item is currently exposed in the state. All items previously passed to setState are guaranteed to never be returned from the hook, so they are disposed at that time.
  • When the hook unmounts, all disposable items passed to setState are disposed.
const {
  state,
  setState,
}: {
  state: T | null;
  setState: (pair: ItemCleanupPair<T>) => void;
} = useUpdatableDisposableState<T>(options);

useUpdatableDisposableClearableState

useUpdatableDisposableState with a third member, clearState:

  • clearState returns the state to UNASSIGNED_STATE. The item that was in state is disposed on the next commit, exactly as if setState had superseded it.
  • clearState throws if called before the initial commit, as setState does.
const {
  state,
  setState,
  clearState,
}: {
  state: T | UnassignedState;
  setState: (pair: ItemCleanupPair<T>) => void;
  clearState: () => void;
} = useUpdatableDisposableClearableState<T>();

useDisposableArray

A hook that holds an array of disposable items, built on useUpdatableDisposableState.

  • Returns an { entries, setEntries } object. entries is the array of ItemCleanupPairs the current render shows. It is empty until the first setEntries call commits. The hook owns each pair's cleanup, so do not call it.
  • setEntries(nextEntries) stores nextEntries. Older arrays are released when a newer array commits, and the rest on unmount. Releasing an array calls the cleanup of each of its pairs once.
  • Every pair in nextEntries must be acquired by the caller for that call, and the hook owns it from then on. To keep an item of entries, take a new reference to it, for example with cloneIfNotDisposed() on a ReferenceCountedPointer from @isograph/reference-counted-pointer. Never pass a pair of entries itself.
  • Build nextEntries from the entries of the same render. If that render is stale, because its entries were released after a newer array committed, cloneIfNotDisposed() returns null. Release what you acquired and do not call setEntries.
  • Two setEntries calls before a commit both build on the same entries, and the second replaces the first.
  • setEntries throws if called before the initial commit, as setState does. It stores nothing in that case, so the caller still owns nextEntries.
const {
  entries,
  setEntries,
}: {
  entries: ReadonlyArray<ItemCleanupPair<T>>;
  setEntries: (nextEntries: ReadonlyArray<ItemCleanupPair<T>>) => void;
} = useDisposableArray<T>();

For example, to keep at most three items, store reference-counted pointers, take a new reference to each item that stays, and add the new one:

const { entries, setEntries } =
  useDisposableArray<ReferenceCountedPointer<Item>>();

function addItem(pair: ItemCleanupPair<Item>) {
  const kept: Array<ItemCleanupPair<ReferenceCountedPointer<Item>>> = [];
  for (const entry of entries.slice(-2)) {
    const clone = entry[0].cloneIfNotDisposed();
    if (clone == null) {
      // This render is stale.
      for (const keptEntry of kept) {
        keptEntry[1]();
      }
      pair[1]();
      return;
    }
    kept.push(clone);
  }
  setEntries([...kept, createReferenceCountedPointer(pair)]);
}

// Read an item with entries[index][0].getItemIfNotDisposed().

useDisposableState

This could properly be called useLazyUpdatableDisposableState, but that's quite long!

A hook that combines the behavior of the previous two hooks:

const {
  state,
  setState,
}: {
  state: T;
  setState: (pair: ItemCleanupPair<T>) => void;
} = useDisposableState<T>(parentCache, factory, options);

Miscellaneous notes

Runtime overhead

  • The hooks in this library are generic, and the type of the disposable items T is mostly as unconstrained as possible.
    • The only constraint we impose on T is to disallow T from including the value UNASSIGNED_STATE. This is for primarily for ergonomic purposes. However, it does prevent some runtime overhead.
  • This incurs some runtime overhead. In particular, it means we need to keep track of an index (and create a new short-lived object) to distinguish items that can overthise be === to each other. Consider, a component that uses useDisposableState or useUpdatableDiposableState. If we execute setState([1, cleanup1]) followed by setState([1, cleanup2]), we would expect cleanup1 to be called when the hook commits. This index is required to distinguish those two, otherwise indistinguishable items.
    • This problem also occurs if disposable items are re-used, but their cleanup functions are distinct. That can occur if items are shared references held in a reference counted wrapper!
  • However, client libraries may not require this flexbility! For example, if every disposable item is a newly-created object, then all disposable items are !== to each other!
  • A future version of this library should expose alternative hooks that disallow null and do away with the above check. They may be