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.tsx2import { useFetch } from "heliumts/client";3import { getTasks } from "../rpc.generated";45function TaskList() {6 const { data, isLoading, error } = useFetch(getTasks, { status: "open" });78 if (isLoading) return <div>Loading...</div>;9 if (error) return <div>Error: {error}</div>;1011 return (12 <ul>13 {data?.map((task) => (14 <li key={task.id}>{task.name}</li>15 ))}16 </ul>17 );18}
Return Values
| Property | Type | Description |
|---|---|---|
data | TResult | undefined | The fetched data (typed based on server method return type) |
isLoading | boolean | Whether a fetch is in progress |
error | string | null | Error message if the fetch failed |
stats | RpcStats | null | Rate-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});
| Option | Type | Default | Description |
|---|---|---|---|
ttl | number | 300000 (5 min) | Time-to-live for cached data in milliseconds. Expired data is refreshed on the next fetch trigger; TTL does not start polling. |
refetchOnWindowFocus | boolean | true | Automatically refetch when the browser tab becomes visible or window regains focus. |
showLoaderOnRefocus | boolean | false | Whether to show loading state during focus-triggered refetches. When false, data updates silently in the background without showing a loader. |
showLoaderOnInvalidate | boolean | false | Whether to show loading state during cache invalidation refetches. When false, data updates silently in the background without showing a loader. |
enabled | boolean | true | Set to false to disable automatic fetching. Useful for conditional fetching when a required value isn't available yet. |