An open API service indexing awesome lists of open source software.

https://github.com/zackify/react-suspendable

a function that can wrap any async action and make it compatible with suspense
https://github.com/zackify/react-suspendable

Last synced: about 2 months ago
JSON representation

a function that can wrap any async action and make it compatible with suspense

Awesome Lists containing this project

README

          

# Suspender

Not really meant for anything serious yet, and may never be.

This is a package that wraps async functions to be used with Suspense. You can see in the example folder how that looks.

### Run the example

The example contains code that helped me get more familiar with suspense data loading. Take a look at the example/src directory.

```
yarn
yarn start

// in another tab
cd example
yarn
yarn start
```

## Basic fetch

```js
import { suspender } from 'react-suspender';

const getData = async type => {
if (type === 'success') {
let response = await fetch('https://test.com');
let json = await response.json();
return json;
}
};

const initialRequest = suspender(getData, 'success');

const App = () => {
let [user, setUser] = React.useState(initialRequest);
return (
sidebar loader...}>


);
};

const SomeComponent = ({ user }) => {
let { data, args } = user.read();
//args are what was passed in to the suspender / async function as the second argument
// data is the resulting promise result
return null;
};
```

## Error Handling

See `examples/src/TestContent.tsx`. You can wrap your component in an error boundary, and then you will be able to render something if an async request throws a promise rejection

## TypeScript example

```js
type User = { name: string };
// initial request must be created outside the render, or else you will be in an infinite loop
const initialRequest = suspender < User > (getData, 'success');

const App = () => {
let [user, setUser] = React.useState(initialRequest);
return (
sidebar loader...}>


);
};

// Example using TS types with suspense
type SidebarProps = {
user: Suspender,
};

const Sidebar = ({ user }: SidebarProps) => {
const { data } = user.read();
// data is correctly typed using the User type above
return null;
};
```