@intrnl/react-asset
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

react-asset

Resource-based data fetching library for React.

Requires React 18 and up

Usage

import { createAsset } from '@intrnl/react-asset';


let posts = createAsset(async (id) => {
  let response = await fetch(`https://jsonplaceholder.typicode.com/posts/${id}`);
  return response.json();
});

function App () {
  let [id, setId] = React.useState(1);

  let resource = posts.use(id);

  return (
    <React.Fragment>
      <React.Suspense fallback={<PostFallback />}>
        <Post resource={resource} />
      </React.Suspense>
    </React.Fragment>
  );
}

function Post ({ resource }) {
  let { id, title, body } = resource.read();

  return (
    <div className='post'>
      <h1>{title}</h1>
      <p>{body}</p>
    </div>
  );
}

Readme

Keywords

none

Package Sidebar

Install

npm i @intrnl/react-asset

Weekly Downloads

0

Version

0.1.1

License

MIT

Unpacked Size

5.28 kB

Total Files

6

Last publish

Collaborators

  • intrnl