0.0.3 • Published 5 years ago

@softbind/hook-use-fetch v0.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

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>
  );
};