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

react-suspense-boundary

v3.0.0

Published

A boundary component working with suspense and error

Downloads

271

Readme

react-suspense-boundary

A boundary component working with suspense and error

Version 2.x is implemented on use-sync-external-store to align with future official suspense data fetching.

Install

npm install react-suspense-boundary

Demo

See online demo here: https://ecomfe.github.io/react-suspense-boundary/

You can start demo app yourself by executing:

npm start

Usage

Basic

import {Boundary, CacheProvider, useResource} from 'react-suspense-boundary';

// Create or import an async function
const fetchInfo = ({id}) => fetch(`/info/${id}`).then(response => response.json());

// Implement your presentational component
function Info({id}) {
    // Call `useResource` to fetch, note the return value is an array
    const [info] = useResource(fetchInfo, {id});

    // There is no `loading` branch, push returned object immediately to render
    return (
        <div>
            {info.id}: {info.name}
        </div>
    );
};

// Data is stored inside `CacheProvider`, suspending state is controlled with `Boundary`
export default function App() => (
    <CacheProvider>
        <Boundary>
            <Info />
        </Boundary>
    </CacheProvider>
);

CacheProvider

CacheProvider is by its name a cache context where we store all resources loaded by its children.

The simpliest way to use CacheProvider is to provider an application level top cache:

import {render} from 'react-dom';
import {CacheProvider} from 'react-suspense-boundary';
import {App} from './components/App';

render(
    <CacheProvider>
        <App />
    </CacheProvider>,
    document.getElementById('root')
);

For some more complex applications, you may want to restrict data caching in a smaller scope, e.g. route level, and expire cached responses on unmount, you can put CacheProvider anywhere you want to make a shared cache.

Boundary

Boundary components defines a boundary in your view, within a boundary all async resource fetchings and errors are collected to form a loading or error indicator.

Usually we would have mulitple Boundary inside a CacheProvider, that is, users see different sections loading individually, but all resources are shared.

A Boundary component receives props below:

interface RenderErrorOptions {
    recover: () => void;
}

interface BoundaryProps {
    // When any of async progress is pending, boundary will render this element
    pendingFallback: ReactNode;
    // When any error are received, will render this function
    renderError(error: Error, options: RenderErrorOptions): ReactNode;
    // When any error are catched, will call this function
    onErrorCaught(error: Error, info: ErrorInfo): void;
}

useResource

The useResource hook is used to inspect an async function within a boundary:

type Resource<T> = [
    T,
    {
        expire(): void;
        refresh(): void;
    }
];

function useResource<I, O>(action: (input: I) => Promise<O>, params: I): Resource<O>;
function useConstantResource<O>(action: () => Promise<O>): Resource<O>;

Unlike other async hooks, useResource returns the result "immediately", there is no pending or loading state, no exception will throw.

Other than the result itself, the second object of useResource's returned array is a a bunch of functions to manually control the cache:

  • expire will immediately remove the cached result, causing the upper Boundary to be pending until action is resolved the next time.
  • refresh is a function to run action again without removing previously cached result.

Default configuration

BoundaryConfigProvider provides default configurations to pendingFallback, renderError and onErrorCaught props.

import {Spin} from 'antd';
import {BoundaryConfigProvider} from 'react-suspense-boundary';

const defaultPendingFallback = <Spin />;

const defaultRenderError = error => (
    <div>
        {error.message}
    </div>
);

const App = () => {
    <BoundaryConfigProvider
        pendingFallback={defaultPendingFallback}
        renderError={defaultRenderError}
    >
        {/* All Boundary elements inside it receives default configurations */}
    </BoundaryConfigProvider>
}

Preload

Preload is much like resource fetching, they can be "immediately" fired within a render function:

function usePreloadResource<I, O>(action: (input: I) => Promise<O>, params: I): void;
function usePreloadConstantResource<O>(action: () => Promise<O>): void;

Preload fires resource fetching process but not abort current render.

You can also get a preload function using usePreloadCallback hook to preload any resources in effect or event handlers:

const preload = usePreloadCallback();

<Button onMouseEnter={() => preload(fetchList, {pageIndex: currentPageIndex + 1})}>
    Next Page
</Button>

Create Your Own Cache

react-suspense-boundary's built-in CacheProvider references a single context type, that is, you are unable to access multiple caches in a single component:

<CacheProvider>
    <div>
        <CacheProvider>
            <MyResource />
        </CacheProvider>
    </div>
</CacheProvider>

By default, there is no way to a make MyResource to load one resource into the outer CacheProvider and another into the inner CacheProvider.

To solve this issue, we provide a create() function to create a custom set of providers and hooks, with a different context type so that you can use them simultaneously:

import {CacheProvider, create, useConstantResource} from 'react-suspense-boundary';

const {
    CacheProvider: GlobalCacheProvider,
    useConstantResource: useGlobalConstantResource,
} = create();

function MyResource() {
    // Put current user resource into global cache
    const [currentUser] = useGlobalConstantResource(fetchCurrentUser);
    // And other resources into local one
    const [dataSource] = useConstantResource(fetchList);

    return (
        // ...
    );
}

<GlobalCacheProvider>
    <CacheProvider>
        <MyResource />
    </CacheProvider>
</GlobalCacheProvider>

create function also accepts an option object to customize context's display name:

interface CreateOptions {
    cacheContextDisplayName?: string;
    configContextDisplayName?: string;
}