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

1.0.7 • Public • Published

react-loader

Simple data loader hooks that use Suspense.

Image from Gyazo

Usage

import { render } from "react-dom";
import { Suspense, useEffect, useState } from "react";
import { Cache, useLoader } from "@naporin0624/react-loader";

import Nprogress from "nprogress";
import "nprogress/nprogress.css";

const timeout = (ms: number) => {
  return new Promise() < number > ((resolve) => setTimeout(() => resolve(ms), ms));
};

const App = () => {
  const [count, setCount] = useState(100);
  const data = useLoader(`timeout_${count}`, () => timeout(count));

  return (
    <div>
      <input type="number" value={count} onChange={(e) => setCount(parseInt(e.target.value, 10))} step={100} min={0} />

      <p>result: {data}</p>
    </div>
  );
};

const Fallback = () => {
  useEffect(() => {
    Nprogress.start();
    return () => {
      Nprogress.done();
    };
  }, []);
  return null;
};

render(
  <Cache>
    <Suspense fallback={<Fallback />}>
      <Cache>
        <App />
      </Cache>
    </Suspense>
  </Cache>,
  document.getElementById("root")
);

LICENSE

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @naporin0624/react-loader

Weekly Downloads

26

Version

1.0.7

License

MIT

Unpacked Size

12.6 kB

Total Files

5

Last publish

Collaborators

  • naporin0624