# react-suspensive

> Promise wrapper for render-as-you-fetch

Latest version **0.1.12** (published 2022-07-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-suspensive
pnpm add react-suspensive
yarn add react-suspensive
bun add react-suspensive
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.12 |
| Published | 2022-07-31 |
| First published | 2020-09-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 23.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Kengo Nakatsuka |
| Maintainers | nak2k |
| Keywords | react |

## Links

- npm: https://www.npmjs.com/package/react-suspensive
- Repository: https://github.com/nak2k/node-react-suspensive
- Issues: https://github.com/nak2k/node-react-suspensive/issues
- npm.io page: https://npm.io/package/react-suspensive

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.1.12 (latest) — 2022-07-31
- 0.1.11 — 2022-04-06
- 0.1.10 — 2021-08-28
- 0.1.9 — 2021-07-04
- 0.1.8 — 2021-05-31
- 0.1.7 — 2020-12-10
- 0.1.6 — 2020-12-01
- 0.1.5 — 2020-12-01
- 0.1.4 — 2020-11-30
- 0.1.3 — 2020-11-26
- 0.1.2 — 2020-10-24
- 0.1.1 — 2020-10-24
- 0.1.0 — 2020-09-21

## README

# react-suspensive

Promise wrapper for render-as-you-fetch.

## Installation

```
npm i react-suspensive
```

## Usage

### Suspensive

`Suspensive` wraps `Promise`, and throw an exception if you try to get an unprepared value.
It can be combined with `Suspepense`.

``` typescript
import { Suspensive } from 'react-suspensive';

function ResourceWithLoading() {
  const resource = new Suspensive(fetch(...));

  return (
    <Suspense fallback={<Loading />}>
      <Resource resource={resource} />
    </Suspense>
  );
}

function Resource(props: { resource: Suspensive<Resource>}) {
  // If the value not prepared, throw an exception at the following line.
  const resource = props.resource.value;

  return (
    <>{ resource }</>
  );
}
```

If give the function that returns `Promise` instead of `Promise`,
`Suspensive` calls the function when the value is needed.

``` typescript
function ResourceWithLoading() {
  // Give the function instead of Promise.
  const resource = new Suspensive(() => fetch(...));

  return (
    <Suspense fallback={<Loading />}>
      <Resource resource={resource} />
    </Suspense>
  );
}

function Resource(props: { resource: Suspensive<Resource>}) {
  // When getting the value first, the above function is called here
  // and the returned promise is thrown.
  const resource = props.resource.value;
}
```

`Suspensive` can wrap a value other than `Promise` also.
In this case, no exceptions are thrown when getting the value.

### Suspensive#set

`set` method sets new `Promise` or new value to `Suspensive`.

``` typescript
function Resource(props: { resource: Suspensive<Resource>}) {
  const resource = props.resource.value;

  return (
    <>
      { resource }
      <Button onClick={() => {
        props.resource.set(fetch(...));
      }}>Reload</Button>
    </>
  );
}
```

When setting new value other than `Promise`, the `value` property can be used as the shorthand.

``` typescript
function Counter(props: { counter: Suspensive<number>}) {
  const { counter } = props;

return (
    <>
      { counter.value }
      <Button onClick={() => {
        counter.value = counter.value + 1;
      }}>Increment</Button>
    </>
  );
}
```

`Suspensive` is observable, so when new `Promise` or value is set, it notify to observers.

### useObserver

`useObserver` observes `Suspensive`, and re-renders the component that calls it
when `Suspensive` is set new `Promise` or value.

``` typescript
import { Suspensive, useObserver } from 'react-suspensive';

function ResourceWithLoading() {
  const resource = new Suspensive(() => fetch(...));

  // Re-render this component, when the reload button in Resource component is clicked.
  useObserver(resource);

  return (
    <Suspense fallback={<Loading />}>
      <Resource resource={resource} />
    </Suspense>
  );
}

function Resource(props: { resource: Suspensive<Resource>}) {
  const resource = props.resource.value;

  return (
    <>
      { resource }
      <Button onClick={() => {
        props.resource.set(fetch(...));
      }}>Reload</Button>
    </>
  );
}
```

### Observer

`Observer` is component version of `useObserver`.
This is useful when re-render part of component tree.

### Wait

`Wait` waits `Suspensive` value with using `Suspense`, and render the resolved value.

After `Suspensive` is resolved, the `render` prop is called with the resolved value.
While waiting, it shows the `fallback` prop.

``` typescript
import { Suspensive, Wait } from 'react-suspensive';

const value = new Suspensive(fetch(...));

<Wait suspensive={value}
  fallback={<div>Loading...</div>}
  render={value =>
    <MyComponent value={value} />
  } />
```

If the `fallback` prop omits, the default value, which can be set using
`setDefaultFallback()`, will be used.

``` typescript
import { Suspensive, Wait, setDefaultFallback } from 'react-suspensive';

setDefaultFallback(<MyLoading />);

const value = new Suspensive(fetch(...));

<Wait suspensive={value} render={value =>
  <MyComponent value={value} />
} />
```

The `renderAlways` prop can be used instead of `render` and `fallback` props.
It renders contents based on waiting status and the resolved value.

``` typescript
import { Suspensive, Wait } from 'react-suspensive';

const value = new Suspensive(fetch(...));

<Wait suspensive={value} renderAlways={(waiting, value) =>
  <Button onClick={reload} disabled={waiting}>Reload</Button>
  { waiting || <MyComponent value={value} /> }
} />
```

`Wait` observes `Suspensive`, so re-render automatically when setting new `Promise` or value.

``` typescript
import { Suspensive, Wait } from 'react-suspensive';

async function fetchMyResource() { ... };

const suspensive = new Suspensive(fetchMyResrouce);

<Wait suspensive={suspensive} render={value =>
  <>
    <MyComponent value={value} />
    <Button onClick={() => {
      // Wait re-renders with new Promise.
      suspensive.set(fetchMyResource);
    }}>Reload</Button>
  </>
} />
```

If the `transient` attribute is specified, `Wait` renders the previous value until new `Promise` is resolved.
And arguments of the `render` function is added a boolean value whether this transition is in progress or not.

``` typescript
<Wait suspensive={suspensive} transient render={(value, pending) =>
  <>
    <MyComponent value={value} />
    <Button disabled={pending} onClick={() => {
      suspensive.set(fetchMyResource);
    }}>Reload</Button>
  </>
} />
```

## Examples

- [counter](examples/counter)

## License

MIT

---
_Source: https://npm.io/package/react-suspensive · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
