[READ-ONLY] Mirror of https://github.com/wojtekmaj/jotai-loadable. A tiny helper that turns async Jotai atoms into loadable atoms
0

Configure Feed

Select the types of activity you want to include in your feed.

TypeScript 91.5%
JavaScript 6.8%
Other 1.7%
21 1 2

Clone this repository

https://tangled.org/wojtekmaj.pl/jotai-loadable https://tangled.org/did:plc:26gbly3dd6zbph7c3qv5bohx
git@tangled.org:wojtekmaj.pl/jotai-loadable git@tangled.org:did:plc:26gbly3dd6zbph7c3qv5bohx

For self-hosted knots, clone URLs may differ based on your setup.



README.md

npm downloads CI

Jotai-Loadable#

Turn an async Jotai atom into a loadable atom with loading, hasData, and hasError states.

tl;dr#

  • Install by executing npm install jotai jotai-loadable or yarn add jotai jotai-loadable.
  • Import by adding import { loadable } from 'jotai-loadable'.
  • Use it by writing const loadableAtom = loadable(asyncAtom).

Examples#

import { atom } from 'jotai';
import { loadable } from 'jotai-loadable';

const asyncAtom = atom(async () => {
  const response = await fetch('/api/user');
  return response.json() as Promise<{ name: string }>;
});

const loadableAtom = loadable(asyncAtom);

// {
//   state: 'loading'
// }
//
// {
//   state: 'hasData',
//   data: { name: 'Wojciech' }
// }
//
// {
//   state: 'hasError',
//   error: Error
// }

License#

The MIT License.

Author#

Wojciech Maj Wojciech Maj