0.0.2 • Published 12 months ago

@react-noui/use-fetch v0.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
12 months ago

use-promise

gzip size npm version PRs Welcome

use-promise helps cache responses from network calls. Supports React.Suspense.

Installation

yarn add @react-noui/use-promise

Or with npm

npm install --save @react-noui/use-promise

Setup

// App.tsx
const AppCache = createReactCache();

function App() {
  return (
    <AppCache.Provider>
      <FetchesTodos />
    </AppCache.Provider>
  )
}

ReactDOM.createRoot(document.getElementById('root')).render(<App />);

Example

This example implements FetchesTodos from the above example. Supported by React.Suspense.

import { useFetch } from '@react-noui/use-promise';

type Todo = {
  id: number;
  title: string;
  completed: boolean;
  userId: number;
}

function FetchesTodos() {
  return (
    <React.Suspense fallback={<FetchesTodosLoading />}>
      <FetchesTodosLazy />
    </React.Suspense>
  );
}

function FetchesTodosLazy() {
  const todos = useFetch('https://jsonplaceholder.typicode.com/todos');
  // Todo[]
}

function FetchesTodosLoading() {
  return <div>Loading Todos...</div>
}
0.0.2

12 months ago

0.0.1

12 months ago