Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

README.md

59 lines1.8 KB

1# @tanstack/preact-db
2​
3Preact hooks for TanStack DB. See [TanStack/db](https://github.com/TanStack/db) for more details.
4​
5## Installation
6​
7```sh
8npm install @tanstack/preact-db
9```
10​
11`@tanstack/preact-db` supports Preact 10 and later and ships as ES modules.
12​
13Queries read concrete source Collections. Preact has no `DbProvider` yet, so it does not bind collection descriptors or read from a `DbClient`; a query over a descriptor throws when it is consumed. Use `@tanstack/react-db` or `@tanstack/svelte-db` for `DbClient`-based SSR and hydration.
14​
15## Usage
16​
17```tsx
18import { eq, useLiveQuery } from '@tanstack/preact-db'
19​
20function TodoList({ userId }: { userId?: string }) {
21 const { data, isLoading } = useLiveQuery((q) =>
22 userId
23 ? q
24 .from({ todos: todosCollection })
25 .where(({ todos }) => eq(todos.userId, userId))
26 : undefined,
27 )
28​
29 if (isLoading) return <div>Loading...</div>
30 return (
31 <ul>
32 {data?.map((todo) => (
33 <li key={todo.id}>{todo.text}</li>
34 ))}
35 </ul>
36 )
38```
39​
40`useLiveQuery` accepts a query callback, a `{ query }` config object, or an
41existing live-query Collection. Return `undefined` or `null` from the callback
42to disable the query. `useLiveInfiniteQuery` pages an ordered live query.
43​
44The hooks derive each query's identity from its structured IR, so values the
45callback captures recompile the query when they change. A query that uses
46functional variants such as `fn.where` has no stable IR hash and throws
47`UnhashableQueryIRError` unless a config object names it with `queryKey`:
48​
49```ts
50const { data } = useLiveQuery({
51 queryKey: [`todos-by-text`, search],
52 query: (q) =>
53 q
54 .from({ todos: todosCollection })
55 .fn.where(({ todos }) => todos.text.includes(search)),
56})
57```
58​
59See the [Preact adapter documentation](https://tanstack.com/db/latest/docs/framework/preact/overview) for more.