resuspend
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-rc10 • Public • Published

Resuspend Logo

resuspend

this unassuming library provides a <Suspension/> component which is designed to work together w/ the <Suspense/> component from the React API in order to facilitate whatever suspension logic you may wish to implement in your React app, with an initial focus on content fulfillment and user experience pacing as the primary use cases

npm node license downloads Travis

getting started

to install the latest stable version:

npm i resuspend -S

to import it into a module:

import { Suspension } from 'resuspend';

basic example

here's a simple example of a <DramaticPause/> component that waits for some amount of time specified by its duration prop before finally rendering its children prop:

import { Suspense, useState, useEffect } from 'react';
import { Suspension } from 'resuspend';

export function DramaticPause(props) {
  // state mapped to the activation status
  const [show, setShow] = useState(!props.duration);

  // activation effect - called when the activation status is set to active
  useEffect(
    function waitAndThenShow() {
      if (!show) {
        const timeoutId = setTimeout(() => setShow(true), props.duration);

        // cleanup callback
        return () => clearTimeout(timeoutId);
      }
    },
    [show, props.duration]
  );

  return (
    <Suspense fallback={<>[pausing dramatically...]</>}>
      <Suspension active={!show}>{props.children}</Suspension>
    </Suspense>
  );
}

try it out in a live editor via CodeSandbox ✨

props reference

as you can see here above w/ the <Suspension/> component working in conjuntion w/ the useEffect hook:

  • the active prop is used to declare whether its activation status is active or inactive

  • the waitAndTheShow effect is used to declare an activation effect to specify what happens when its activation status is switched to active or inactive

essentially, the suspension logic is composed of and encapsulated by these values.

data fetching example

more commonly, your suspension logic will probably involve fetching some data from a remote API service and rendering it in some kind of way.

here's a simple example of a <UserStatus> component that does just that:

import { Suspense, useState, useEffect } from 'react';
import { Suspension } from 'resuspend';

export function UserStatus(props) {
  // state mapped to the activation status
  const [status, setStatus] = useState();

  // activation effect - called when the activation status is set to active
  useEffect(
    function fetchStatus() {
      if (!status) {
        fetch(`/api/status/${props.userId}`)
          .then((response) => response.json())
          .then((data) => setStatus(data.status));
      }
      x;
    },
    [status, props.userId]
  );

  return (
    <Suspense fallback={<>[fetching status...]</>}>
      <Suspension active={!status}>{status}</Suspension>
    </Suspense>
  );
}

try it out now in a live editor via CodeSandbox ✨

activation

to set the activation status to active, you would simply assign the prop active={true}:

<Suspense fallback={<>[pausing dramatically...]</>}>
  <Suspension active={true}>
    you will not be seeing this text (except here in code) 👀
  </Suspension>
</Suspense>

that's all you need to activate a <Suspension/> component, consequently triggering its ancestral <Suspense/> component and causing it to suspend updating and rendering of its children and instead rendering its fallback prop.

inactivation

likewise to set the activation status to inactive, you would simply assign the prop active={false}:

<Suspense fallback={<>[pausing dramatically...]</>}>
  <Suspension active={false}>you will be seeing this text 👀</Suspension>
</Suspense>

you may also assign a callback to the active prop and it will be evaluated to active (for truthy values) or inactive (for falsey values.)

<Suspense fallback={<>[pausing randomly and dramatically...]</>}>
  <Suspension active={() => Math.random() >= 0.5}>
    you will maybe be seeing this text 👀
  </Suspension>
</Suspense>

reactivation

in general, using a basic callback is a fine way to switch an activation status from inactive to active. this callback can simply be attached to a useEffect hook or perhaps to some UI element event, e.g. a button click:

import { Suspense, useState, useCallback } from 'react';
import { Suspension } from 'resuspend';

export function DramaticPause(props) {
  // state mapped to the activation status
  const [show, setShow] = useState(!props.duration);

  // activation effect callback - called when the activation status is set to active
  useEffect(
    function waitAndThenShow() {
      if (!show) {
        const timeoutId = setTimeout(() => setShow(true), props.duration);

        // cleanup callback - called when the activation status is switched to inactive
        return () => clearTimeout(timeoutId);
      }
    },
    [show, props.duration]
  );

  // (re)activation callback
  const reload = useCallback(() => setShow(false));

  return (
    <>
      <Suspense fallback={<>[pausing dramatically...]</>}>
        <Suspension active={!show}>{props.children}</Suspension>
      </Suspense>
      <button disabled={!show} onClick={reload}>
        Reload
      </button>
    </>
  );
}

try it out now in a live editor via CodeSandbox ✨

lifecycle

as implied so far, a <Suspension/> component may be activated, deactivated, then reactivated and deactivated again, and so on until it's unmounted.

this flow of state encapsulates the lifecycle of <Suspension/> components, and is designed intentionally to provide a level of continuity and reusability you'd expect from a decent React component library (especially one whose name starts with re-!)

Package Sidebar

Install

npm i resuspend

Weekly Downloads

0

Version

1.0.0-rc10

License

MIT

Unpacked Size

43.4 kB

Total Files

18

Last publish

Collaborators

  • json2d