HeliumTS is under active development and may have bugs or breaking changes. Report issues on GitHub.

Data Fetching

HeliumTS provides the useFetch hook for data fetching. It communicates with the server over HTTP RPC and shares cached results between matching calls.

useFetch

The useFetch hook automatically fetches data from a server method and caches the result. It's designed for reading/querying data.

Basic Usage

1// src/components/TaskList.tsx
2import { useFetch } from "heliumts/client";
3import { getTasks } from "../rpc.generated";
4
5function TaskList() {
6 const { data, isLoading, error } = useFetch(getTasks, { status: "open" });
7
8 if (isLoading) return <div>Loading...</div>;
9 if (error) return <div>Error: {error}</div>;
10
11 return (
12 <ul>
13 {data?.map((task) => (
14 <li key={task.id}>{task.name}</li>
15 ))}
16 </ul>
17 );
18}

Return Values

PropertyTypeDescription
dataTResult | undefinedThe fetched data (typed based on server method return type)
isLoadingbooleanWhether a fetch is in progress
errorstring | nullError message if the fetch failed
statsRpcStats | nullRate-limit statistics: remaining requests and seconds until the limit resets
refetch(showLoader?: boolean) => Promise<TResult | undefined>Function to manually trigger a refetch

Concurrent requests with the same method and arguments share one in-flight fetch. Changing arguments prevents older responses from replacing the current result. Cache invalidation refreshes all mounted readers for that method; failed fetches can be retried with refetch().

Options

The useFetch hook accepts an optional third parameter for controlling caching and refetch behavior:

1const { data, isLoading } = useFetch(method, args, {
2 ttl: 30000,
3 refetchOnWindowFocus: true,
4 showLoaderOnRefocus: false,
5 showLoaderOnInvalidate: false,
6 enabled: true,
7});
OptionTypeDefaultDescription
ttlnumber300000 (5 min)Time-to-live for cached data in milliseconds. Expired data is refreshed on the next fetch trigger; TTL does not start polling.
refetchOnWindowFocusbooleantrueAutomatically refetch when the browser tab becomes visible or window regains focus.
showLoaderOnRefocusbooleanfalseWhether to show loading state during focus-triggered refetches. When false, data updates silently in the background without showing a loader.
showLoaderOnInvalidatebooleanfalseWhether to show loading state during cache invalidation refetches. When false, data updates silently in the background without showing a loader.
enabledbooleantrueSet to false to disable automatic fetching. Useful for conditional fetching when a required value isn't available yet.