Skip to main content
Version: v10.5.1

React overview

Pass a request to a hook, get typed data, error and loading state back. No query keys or fetcher functions: the request you defined with core already knows its endpoint, types and cache key.

users.tsx
import { useFetch } from "@hyper-fetch/react";

import { getUsers } from "./api/users";

export const Users = () => {
const { data, loading, error } = useFetch(getUsers);

if (loading && !data) return <p>Loading...</p>;
if (error) return <p>{error.message}</p>;

return <ul>{data?.map((user) => <li key={user.id}>{user.name}</li>)}</ul>;
};

data is typed as User[] | null from the request, so the hook needs no generics. The hooks need React 18 or later.

  1. Request

    Defined once, at module level

  2. Hook

    Sends and subscribes

  3. Cache

    One entry, shared by every hook

  4. Component

    Re-renders on the fields it reads

Mount the same hook ten times and the server sees one request.

The hooks​


Global defaults​

Hooks work without a provider. Add <Provider> when every hook should share options, such as refetching when the window regains focus. Options passed to a hook always win. The config provider guide shows the setup.


Common tasks​


Next​