Docs
useFetch

Fetch data with accurate states, caching, and no stale responses using useFetch.

Loading...

Installation

pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-fetch

Usage

"use client"
 
import * as React from "react"
 
import { useFetch } from "@/hooks/use-fetch"
 
import Card from "./card"
 
export function Component() {
  const [count, setCount] = React.useState(1)
 
  const { error, data, loading } = useFetch(
    `https://pokeapi.co/api/v2/pokemon/${count}`
  )
 
  return (
    <section>
      <h1>useFetch</h1>
      <button
        disabled={count < 2}
        className="link"
        onClick={() => setCount((c) => c - 1)}
      >
        Prev
      </button>
      <button className="link" onClick={() => setCount((c) => c + 1)}>
        Next
      </button>
      <Card loading={loading} error={error} data={data} />
    </section>
  )
}

API Reference

Parameters

NameTypeDescriptionDefault ValueOptional
urlstringThe URL to fetch data from.-No
optionsobjectAdditional options for the fetch request, such as headers or request methods.{}Yes

Return Values

NameTypeDescription
stateobjectThe state object containing the fetched data and error status.
state.errorerror | undefinedThe error object if an error occurred during the fetch, otherwise undefined.
state.dataany | undefinedThe fetched data if the fetch was successful, otherwise undefined.
state.loadingbooleanThe loading state of the fetch request.