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

@m4rw3r/react-pause-champ

v0.2.5

Published

Isomorphic React hook providing async-aware stateful variables to components, with Suspense and Server-Side-Rendering support

Downloads

254

Readme

React Pause Champ

Isomorphic React hook providing async-aware stateful variables to components, with Suspense and Server-Side-Rendering support.

Features

  • React useState-like API

    Pass an identifier and an initial value to the useChamp hook and you get the current value and a setter, almost exactly like React's useState!

  • Asynchronous initializers and updates

    Any function passed as either the initial value or an update of a stateful variable can be asynchronous, either through using the async-keyword or by returning a Promise.

  • Suspense

    Asynchronous state initializers and updates will trigger fallback components if wrapped in <Suspense/>-boundaries. For updates this can be managed using React's startTransition.

  • Server-Side-Rendering

    The whole application can render on the server, await asynchronous data, and then hydrate on client. Transparently. With the same application code.

  • Server Streaming

    Server-rendering will wait for all asynchronous initializers/updates outside of any <Suspense/>-boundaries to finish before sending the initial HTML-shell to the client. Any asynchronous initializers wrapped in a <Suspense/>-boundary will send the fallback components to the client as a part of the HTML-shell, and once they have completed they will be streamed to the client, including the stateful data, allowing for a seamless experience.

  • Error Boundary compatibility

    Errors thrown in initializers and updates propagate to the closest Error Boundary, allowing for unified error-handling.

  • Small size

    Around 1kB gzipped without development helpers and server components. Has zero dependencies besides React, and can be treeshaked.

Installation

npm install @m4rw3r/react-pause-champ

Dependencies

  • React 18

Recommended to use createRoot/hydrateRoot on the client to use batching. On the server renderToPipeableStream/renderToReadableStream is required for asynchronous initializations and Suspense support.

Examples

import { useChamp } from "@m4rw3r/react-pause-champ";

/**
 * Plain useState replacement with SSR-support.
 */
function Counter(): JSX.Element {
  const [data, update] = useChamp("my-counter", 0);

  return (
    <div>
      <p>{counter}</p>
      <button onClick={() => update((i) => i + 1)}>Increment</button>
    </div>
  );
}

/**
 * Isomorphic asynchronous fetch with SSR- and Suspense-support.
 */
function Page({ pageId }: { pageId: string }): JSX.Element {
  const [{ title, data }] = useChamp(`page.${pageId}`, async () => {
    const { title, data } = await fetchPageData();

    return { title: title.toUpperCase(), data };
  });

  return (
    <div>
      <h2>{title}</h2>
      <p>{data}</p>
    </div>
  );
}

/**
 * Asynchronous updates.
 */
function ServerCounter(): JSX.Element {
  const [value, update] = useChamp(
    "my-async-counter",
    async () => (await fetchCounter()).value
  );

  return (
    <div>
      <p>{value}</p>
      <button
        onClick={() =>
          update(
            async (old) =>
              (await fetchCounterUpdate({ newValue: old + 1 })).value
          )
        }
      >
        Increment
      </button>
    </div>
  );
}

Frequently Asked Questions

  • Uncaught Error: State '*' is already mounted in another component. when using Hot-Module-Reloading.

    This is caused by reordering components in JSX without using key. When key is skipped components will use the hooks from the previous component mounted in that "slot" instead, which will cause issues with use of useState/useRef/useEffect and so on. Pause Champ uses useRef to track component instances which can and will trigger exceptions if those values are not what it expects.