본문으로 건너뛰기

쿼리

컴포넌트에서 쿼리를 구독하려면 최소한 다음 항목과 함께 useQuery 함수를 호출합니다: //: # 'SubscribeDescription'

import { useQuery } from '@tanstack/solid-query'

function App() {
const todosQuery = useQuery(() => ({
queryKey: ['todos'],
queryFn: fetchTodoList,
}))
}
const todosQuery = useQuery(() => ({
queryKey: ['todos'],
queryFn: fetchTodoList,
}))
import { Switch, Match, For } from 'solid-js'

function Todos() {
const todosQuery = useQuery(() => ({
queryKey: ['todos'],
queryFn: fetchTodoList,
}))

return (
<Switch>
<Match when={todosQuery.isPending}>
<span>Loading...</span>
</Match>
<Match when={todosQuery.isError}>
<span>Error: {todosQuery.error.message}</span>
</Match>
<Match when={todosQuery.isSuccess}>
<ul>
<For each={todosQuery.data}>{(todo) => <li>{todo.title}</li>}</For>
</ul>
</Match>
</Switch>
)
}
import { Switch, Match, For } from 'solid-js'

function Todos() {
const todosQuery = useQuery(() => ({
queryKey: ['todos'],
queryFn: fetchTodoList,
}))

return (
<Switch>
<Match when={todosQuery.status === 'pending'}>
<span>Loading...</span>
</Match>
<Match when={todosQuery.status === 'error'}>
<span>Error: {todosQuery.error.message}</span>
</Match>
<Match when={todosQuery.status === 'success'}>
<ul>
<For each={todosQuery.data}>{(todo) => <li>{todo.title}</li>}</For>
</ul>
</Match>
</Switch>
)
}