diff --git a/docs/framework/preact/reference/functions/useQuery.md b/docs/framework/preact/reference/functions/useQuery.md index 4f002e036d5..2d1320fd0fc 100644 --- a/docs/framework/preact/reference/functions/useQuery.md +++ b/docs/framework/preact/reference/functions/useQuery.md @@ -190,7 +190,7 @@ function Posts() { function useQuery(options, queryClient?): UseQueryResult; ``` -Defined in: [preact-query/src/useQuery.ts:261](https://github.com/TanStack/query/blob/main/packages/preact-query/src/useQuery.ts#L261) +Defined in: [preact-query/src/useQuery.ts:281](https://github.com/TanStack/query/blob/main/packages/preact-query/src/useQuery.ts#L281) ### Type Parameters @@ -264,6 +264,25 @@ function Posts() { } ``` +`select` derives whatever `data` a component needs from the cached value, without changing what's +actually stored in the cache — the cache still holds the full `Post[]`, but `data` here is a `number`: +```tsx +import { useQuery } from '@tanstack/preact-query' + +function PostCount() { + const { data, isPending, isError, error } = useQuery({ + queryKey: ['posts'], + queryFn: fetchPosts, + select: (posts) => posts.length, + }) + + if (isPending) return 'Loading...' + if (isError) return Error: {error.message} + + return {data} posts +} +``` + A dependent query, only enabled once `postId` is set — use `isLoading`, not `isPending`, so the loading state doesn't show while the query is disabled: ```tsx diff --git a/packages/preact-query/src/useQuery.ts b/packages/preact-query/src/useQuery.ts index dfa19538c15..13fff59759f 100644 --- a/packages/preact-query/src/useQuery.ts +++ b/packages/preact-query/src/useQuery.ts @@ -160,6 +160,26 @@ export function useQuery< * ``` * * @example + * `select` derives whatever `data` a component needs from the cached value, without changing what's + * actually stored in the cache — the cache still holds the full `Post[]`, but `data` here is a `number`: + * ```tsx + * import { useQuery } from '@tanstack/preact-query' + * + * function PostCount() { + * const { data, isPending, isError, error } = useQuery({ + * queryKey: ['posts'], + * queryFn: fetchPosts, + * select: (posts) => posts.length, + * }) + * + * if (isPending) return 'Loading...' + * if (isError) return Error: {error.message} + * + * return {data} posts + * } + * ``` + * + * @example * A dependent query, only enabled once `postId` is set — use `isLoading`, not `isPending`, so the * loading state doesn't show while the query is disabled: * ```tsx