JSPM

@gdo-bzh/use-fetch

1.0.0-rc.4
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q9125F
  • License MIT

Package Exports

  • @gdo-bzh/use-fetch

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@gdo-bzh/use-fetch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

version

use-fetch

NPM JavaScript Style Guide

this hook is charged to fetch data using the fetch API (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). React Hooks are not be intended for data fetching in React. Instead, a feature called Suspense will be in charge for it. The following walkthrough is nonetheless funny.

Install

yarn add @gdo-bzh/use-fetch react

Usage

import React from 'react'

type User = {
  name: string
  email: string
  avatar: string
}

const Example = () => {
  const {
    state: { isLoading, isError, isAborted, error, data },
    setInput,
    abort
  } = useFetch<User[]>('/getUsers', {}, [])
}

Types

type State<T> = {
  isLoading: boolean
  isError: boolean
  isAborted: boolean
  error?: Error
  data: T
}

/**
 * hook type
 */
export type UseFetch = {
  <T>(input: RequestInfo, init: RequestInit, initialData: T): {
    state: State<T>
    setInput: React.Dispatch<React.SetStateAction<RequestInfo>>
    abort: () => boolean
  }
}

License

MIT © gdo-bzh