함수: useLiveQuery()
호출 시그니처
function useLiveQuery<TContext>(queryFn): Accessor<InferResultType<TContext>> & object;
정의 위치: useLiveQuery.ts:103
쿼리 함수를 사용하여 라이브 쿼리를 생성합니다
타입 매개변수
TContext
TContext extends Context
매개변수
queryFn
(q) => QueryBuilder<TContext>
가져올 데이터를 정의하는 쿼리 함수입니다.
반환값
Suspense를 지원하고 상태 및 상태 정보를 속성으로 제공하는 데이터 반환 접근자입니다
예제
// Basic query with object syntax
const todosQuery = useLiveQuery((q) =>
q.from({ todos: todosCollection })
.where(({ todos }) => eq(todos.completed, false))
.select(({ todos }) => ({ id: todos.id, text: todos.text }))
)
// With dependencies that trigger re-execution
const todosQuery = useLiveQuery(
(q) => q.from({ todos: todosCollection })
.where(({ todos }) => gt(todos.priority, minPriority())),
)
// Join pattern
const personIssues = useLiveQuery((q) =>
q.from({ issues: issueCollection })
.join({ persons: personCollection }, ({ issues, persons }) =>
eq(issues.userId, persons.id)
)
.select(({ issues, persons }) => ({
id: issues.id,
title: issues.title,
userName: persons.name
}))
)
// Handle loading and error states
const todosQuery = useLiveQuery((q) =>
q.from({ todos: todoCollection })
)
return (
<Switch>
<Match when={todosQuery.isLoading}>
<div>Loading...</div>
</Match>
<Match when={todosQuery.isError}>
<div>Error: {todosQuery.status}</div>
</Match>
<Match when={todosQuery.isReady}>
<For each={todosQuery()}>
{(todo) => <li key={todo.id}>{todo.text}</li>}
</For>
</Match>
</Switch>
)
// Use Suspense boundaries
const todosQuery = useLiveQuery((q) =>
q.from({ todos: todoCollection })
)
return (
<Suspense fallback={<div>Loading...</div>}>
<For each={todosQuery()}>
{(todo) => <li key={todo.id}>{todo.text}</li>}
</For>
</Suspense>
)
호출 시그니처
function useLiveQuery<TContext>(queryFn): Accessor<InferResultType<TContext>> & object;
정의 위치: useLiveQuery.ts:122
쿼리 함수를 사용하여 라이브 쿼리를 생성합니다
타입 매개변수
TContext
TContext extends Context
매개변수
queryFn
(q) => QueryBuilder<TContext> | null | undefined
가져올 데이터를 정의하는 쿼리 함수입니다.
반환값
Suspense를 지원하고 상태 및 상태 정보를 속성으로 제공하는 데이터 반환 접근자입니다
예제
// Basic query with object syntax
const todosQuery = useLiveQuery((q) =>
q.from({ todos: todosCollection })
.where(({ todos }) => eq(todos.completed, false))
.select(({ todos }) => ({ id: todos.id, text: todos.text }))
)
// With dependencies that trigger re-execution
const todosQuery = useLiveQuery(
(q) => q.from({ todos: todosCollection })
.where(({ todos }) => gt(todos.priority, minPriority())),
)
// Join pattern
const personIssues = useLiveQuery((q) =>
q.from({ issues: issueCollection })
.join({ persons: personCollection }, ({ issues, persons }) =>
eq(issues.userId, persons.id)
)
.select(({ issues, persons }) => ({
id: issues.id,
title: issues.title,
userName: persons.name
}))
)
// Handle loading and error states
const todosQuery = useLiveQuery((q) =>
q.from({ todos: todoCollection })
)
return (
<Switch>
<Match when={todosQuery.isLoading}>
<div>Loading...</div>
</Match>
<Match when={todosQuery.isError}>
<div>Error: {todosQuery.status}</div>
</Match>
<Match when={todosQuery.isReady}>
<For each={todosQuery()}>
{(todo) => <li key={todo.id}>{todo.text}</li>}
</For>
</Match>
</Switch>
)
// Use Suspense boundaries
const todosQuery = useLiveQuery((q) =>
q.from({ todos: todoCollection })
)
return (
<Suspense fallback={<div>Loading...</div>}>
<For each={todosQuery()}>
{(todo) => <li key={todo.id}>{todo.text}</li>}
</For>
</Suspense>
)
호출 시그니처
function useLiveQuery<TContext>(config): Accessor<InferResultType<TContext>> & object;
정의 위치: useLiveQuery.ts:183
구성 객체를 사용하여 라이브 쿼리를 생성합니다.
타입 매개변수
TContext
TContext extends Context
매개변수
config
Accessor<LiveQueryCollectionConfig<TContext, RootQueryResult<TContext>>>
쿼리와 옵션이 있는 구성 객체입니다.
반환값
Suspense를 지원하고 상태 및 상태 정보를 속성으로 제공하는 데이터 반환 접근자입니다
예제
// Basic config object usage
const todosQuery = useLiveQuery(() => ({
query: (q) => q.from({ todos: todosCollection }),
gcTime: 60000
}))
// With query builder and options
const queryBuilder = new Query()
.from({ persons: collection })
.where(({ persons }) => gt(persons.age, 30))
.select(({ persons }) => ({ id: persons.id, name: persons.name }))
const personsQuery = useLiveQuery(() => ({ query: queryBuilder }))
// Handle all states uniformly
const itemsQuery = useLiveQuery(() => ({
query: (q) => q.from({ items: itemCollection })
}))
return (
<Switch fallback={<div>{itemsQuery().length} items loaded</div>}>
<Match when={itemsQuery.isLoading}>
<div>Loading...</div>
</Match>
<Match when={itemsQuery.isError}>
<div>Something went wrong</div>
</Match>
<Match when={!itemsQuery.isReady}>
<div>Preparing...</div>
</Match>
</Switch>
)
호출 시그니처
function useLiveQuery<TResult, TKey, TUtils>(liveQueryCollection): Accessor<TResult[]> & object;
정의 위치: useLiveQuery.ts:237
기존 라이브 쿼리 컬렉션을 구독합니다.
타입 매개변수
TResult
TResult extends object
TKey
TKey extends string | number
TUtils
TUtils extends Record<string, any>
매개변수
liveQueryCollection
Accessor<Collection<TResult, TKey, TUtils, StandardSchemaV1<unknown, unknown>, TResult> & NonSingleResult>
구독할 미리 생성된 라이브 쿼리 컬렉션입니다.
반환값
Suspense를 지원하고 상태 및 상태 정보를 속성으로 제공하는 데이터 반환 접근자입니다
예제
// Using pre-created live query collection
const myLiveQuery = createLiveQueryCollection((q) =>
q.from({ todos: todosCollection }).where(({ todos }) => eq(todos.active, true))
)
const todosQuery = useLiveQuery(() => myLiveQuery)
// Access collection methods directly
const existingQuery = useLiveQuery(() => existingCollection)
// Use collection for mutations
const handleToggle = (id) => {
existingQuery.collection.update(id, draft => { draft.completed = !draft.completed })
}
// Handle states consistently
const sharedQuery = useLiveQuery(() => sharedCollection)
return (
<Switch fallback={<div><For each={sharedQuery()}>{(item) => <Item key={item.id} {...item} />}</For></div>}>
<Match when={sharedQuery.isLoading}>
<div>Loading...</div>
</Match>
<Match when={sharedQuery.isError}>
<div>Error loading data</div>
</Match>
</Switch>
)
호출 시그니처
function useLiveQuery<TResult, TKey, TUtils>(liveQueryCollection): Accessor<TResult | undefined> & object;
정의 위치: useLiveQuery.ts:262
쿼리 함수를 사용하여 라이브 쿼리를 생성합니다
타입 매개변수
TResult
TResult extends object
TKey
TKey 확장 string | number
TUtils
TUtils 확장 Record<string, any>
매개변수
liveQueryCollection
Accessor<Collection<TResult, TKey, TUtils, StandardSchemaV1<unknown, unknown>, TResult> & SingleResult>
반환값
Suspense를 지원하고 상태 및 상태 정보를 속성으로 제공하는 데이터 반환 접근자입니다
예제
// Basic query with object syntax
const todosQuery = useLiveQuery((q) =>
q.from({ todos: todosCollection })
.where(({ todos }) => eq(todos.completed, false))
.select(({ todos }) => ({ id: todos.id, text: todos.text }))
)
// With dependencies that trigger re-execution
const todosQuery = useLiveQuery(
(q) => q.from({ todos: todosCollection })
.where(({ todos }) => gt(todos.priority, minPriority())),
)
// Join pattern
const personIssues = useLiveQuery((q) =>
q.from({ issues: issueCollection })
.join({ persons: personCollection }, ({ issues, persons }) =>
eq(issues.userId, persons.id)
)
.select(({ issues, persons }) => ({
id: issues.id,
title: issues.title,
userName: persons.name
}))
)
// Handle loading and error states
const todosQuery = useLiveQuery((q) =>
q.from({ todos: todoCollection })
)
return (
<Switch>
<Match when={todosQuery.isLoading}>
<div>Loading...</div>
</Match>
<Match when={todosQuery.isError}>
<div>Error: {todosQuery.status}</div>
</Match>
<Match when={todosQuery.isReady}>
<For each={todosQuery()}>
{(todo) => <li key={todo.id}>{todo.text}</li>}
</For>
</Match>
</Switch>
)
// Use Suspense boundaries
const todosQuery = useLiveQuery((q) =>
q.from({ todos: todoCollection })
)
return (
<Suspense fallback={<div>Loading...</div>}>
<For each={todosQuery()}>
{(todo) => <li key={todo.id}>{todo.text}</li>}
</For>
</Suspense>
)