You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 7f40ee2
Browse filesBrowse the repository at this point in the historyBrowse files
feat(fetchium): add an enabled option to useQuery (#85)
* feat(fetchium): add an `enabled` option to `useQuery`
* fix(docs): remove trailing whitespace from useQuery example
* feat(fetchium): show cached data from disabled `useQuery`
* fix(fetchium): keep disabled `useQuery` cache reads across remounts and param changes
Add an `enabled` option to `useQuery`: `useQuery(Query, params, { enabled })`. It defaults to `true`. A disabled query never fetches or subscribes, but still reads the cache, like TanStack Query's `enabled: false`. With cached data, in memory or in the persistent store within `cacheTime`, it starts resolved with that data, even if stale. Without cached data it stays pending and sends no request. The cached value stays live: entity updates and fetches by enabled readers of the same query show up in it. Setting `enabled` to `true` activates the query with the current params, and doesn't refetch a cache that is still within `staleTime`. For queries without params, pass `undefined` as the second argument.
React hook for fetching a query. Subscribes the component to the query's reactive state, re-rendering when the query result changes. Internally uses Signalium's `useReactive` (deep-by-default in v3) to bridge the reactive signal system with React's rendering cycle.
|`QueryClass`|`new () => T`| The query class to instantiate and execute. Must extend `Query` or `RESTQuery`. |
34
-
|`params`|`ExtractQueryParams<T>`| Parameters matching the query's `params` shape. Optional if the query has no required params. Values can be Signalium `Signal`s for reactive parameter changes. |
|`QueryClass`|`new () => T`| The query class to instantiate and execute. Must extend `Query` or `RESTQuery`. |
35
+
|`params`|`ExtractQueryParams<T>`| Parameters matching the query's `params` shape. Optional if the query has no required params. Values can be Signalium `Signal`s for reactive parameter changes. |
36
+
|`options.enabled`|`boolean`| Defaults to `true`. When `false`, skips fetching and subscribing to the query, but still returns cached data when there is some. |
<div>{enabled?'Loading...':'Select a user to load their profile.'}</div>
152
+
);
153
+
}
154
+
```
155
+
156
+
A disabled query never fetches or subscribes, but it still reads the cache:
157
+
158
+
- If the query has cached data (in memory, or in the persistent store and within its `cacheTime`), the result starts resolved: `isReady: true`, `isResolved: true`, `isPending: false`, with `value` set to the cached data. Stale data is shown as-is. It is not refetched while disabled.
159
+
- If there is no cached data, the result stays `isPending: true` with `value: undefined` and no request is made.
160
+
161
+
The cached value stays reactive. Entity updates and fetches by enabled consumers of the same query appear in the disabled result. Setting `enabled` to `true` activates the query with the current parameters. Setting it back to `false` releases this hook's subscription; other consumers of the same query remain active. For queries without parameters, pass `undefined` as the second argument: `useQuery(GetUsers, undefined, { enabled })`.
162
+
131
163
#### Notes
132
164
133
165
-`useQuery` is a thin wrapper around Signalium v3's `useReactive`, which is deep-by-default. It returns a **structurally-shared snapshot** of the query result, so memoized children that receive subtrees as props keep stable references when the underlying data is unchanged.
0 commit comments