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

https://github.com/explodingcamera/react-sus

Minimal Suspense-based data-fetching
https://github.com/explodingcamera/react-sus

Last synced: 4 months ago
JSON representation

Minimal Suspense-based data-fetching

Awesome Lists containing this project

README

        


react-sus
 


```bash
npm i react-sus
```

Minimal Suspense-based data-fetching (Currently in development and not production-ready)

## Example

TypeScript

```tsx
import { Suspense } from "react";
import { sus } from "react-sus";

interface User {
username: string;
}

const SomeComponent = ({ userID }: { userID: string }) => {
const data = sus(`https://api.example.com/user/${userID}`);
return

{data.username}
;
};

const App = () => (
Loading...}>


);
```

## API

Simplified API

```tsx
type sus = (
key: Key, // anything that can be serialized to a string
fetcher?: Fetcher | undefined,
userConfig?: Config
) => Await;

interface Config {
fetcher: Fetcher; // any promise or fetch-like function
cacheProvider: Cache; // any Map-like function
}
```

Any Component using this function needs to be wrapped in an `` block. Once the promise is started, control will be given back to react until the promise resolves and the actual data is returned.

The key acts as the key for caching the results, so be sure to not reuse the same cache keys for different requests (only relevant when they both use the same cacheProvider).

When an error occours, it will bubble up to the next ``. Because of this, the return value is guaranteed to be valid.

## Configuration

### Context

> will be available at release

### Pre-Loading

> will be available at release

### Cache Busting

> will be available at release

## Inspirations

This project seeks to combine the simplicity of swr with react-query's feature set and suspend-react's suspense support.

- [swr](https://github.com/vercel/swr)
- [suspend-react](https://github.com/pmndrs/suspend-react)
- [react-query](https://github.com/tannerlinsley/react-query)

## Related Projects

- [snowstorm](https://github.com/explodingcamera/snowstorm) - The lightning-fast and minimalist React Tool