npm.io
2.1.5 • Published 5 years ago

reasc

Licence
MIT
Version
2.1.5
Deps
0
Size
74 kB
Vulns
0
Weekly
0

REASC

React Async Component

Installation

with NPM

npm i reasc --save

with YARN

yarn add reasc

Getting Started

import { reasc } from "reasc";
import { useState } from "react";
const UserInfo = reasc(
  { loading: () => <div>Loading...</div> },
  async ({ user }, { delay }) => {
    if (!user) return <div>No data</div>;
    await delay(300);
    const apiUrl = `https://api.github.com/users/${user}`;
    const data = await fetch(apiUrl).then((res) => res.json());
    return <pre>{JSON.stringify(data, null, 2)}</pre>;
  }
);
const App = () => {
  const [user, setUser] = useState("");
  const handleChange = (e) => setUser(e.target.value);
  return (
    <>
      <input onChange={handleChange} placeholder="Enter github username" />
      <UserInfo user={user} />
    </>
  );
};

Examples

Keywords