@softbind/hook-use-fetch
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

useFetch

Installation

npm i @softbind/hook-use-fetch --save

API

useFetch(text)

Arguments

  • url: String | () => Promise<any>: url to api or function which need to return promise.
  • opt: Object: you can pass all options that occur in Request
import { useFetch } from "@softbind/hook-use-fetch";

const PageTemplate = () => {
  const { loading, result, cancel, invoke } = useFetch('http://api.im?query=Dog')

  if (loading) {
    return (
      <span>Loading...</span>
    )
  }

  return (
    <div>
      {result ? JSON.stringify(result), null, 2) : null}
      {loading && <button onClick={invoke}>Refresh</button>}
      {!loading && <button onClick={cancel}>Cancel</button>}
    </div>
  );
};

Package Sidebar

Install

npm i @softbind/hook-use-fetch

Weekly Downloads

0

Version

0.0.3

License

MIT

Unpacked Size

10.6 kB

Total Files

8

Last publish

Collaborators

  • hiddenboox