Skip to content

Documentation / @agentick/core / useData

Function: useData() ​

useData<T>(key, fetcher, deps?, options?): T

Defined in: core/src/hooks/data.ts:56

Fetch and cache async data.

This hook enables the "resolve-then-render" pattern:

  1. First render: throws a promise (signals need for data)
  2. Engine catches, resolves all pending fetches
  3. Second render: returns cached value

If the fetcher rejects, the error is cached for the current deps. The error is re-thrown synchronously on the next render (not as a promise), so the compiler loop terminates cleanly. When deps change the cache invalidates and a fresh fetch is attempted.

Type Parameters ​

T ​

T

Parameters ​

key ​

string

fetcher ​

() => Promise<T>

deps? ​

unknown[]

options? ​

UseDataOptions

Returns ​

T

Examples ​

tsx
const MyComponent = ({ userId }) => {
  // Cached across ticks, refetch when userId changes
  const user = useData('user', () => fetchUser(userId), [userId]);

  // Refetch every tick by including tick in deps
  const { tick } = useTickState();
  const status = useData('status', fetchStatus, [tick]);

  return <Section>{user.name}: {status}</Section>;
};
tsx
// Large or frequently-changing data — re-fetch on hydration instead
const embeddings = useData(
  'embeddings',
  () => fetchEmbeddings(query),
  [query],
  { persist: false },
);

Released under the ISC License.