0
Fork 0
mirror of https://github.com/TanStack/query.git synced 2026-11-16 22:50:20 +00:00
query/docs/framework/solid/guides/query-functions.md
Wonsuk Choi e31bda9136
docs(solid-query): convert guide docs to ref-based with Solid-style code (#10146)
* docs(react-query/guides): add markers for solid-query guide overrides

* docs(solid-query/guides): convert guide docs to ref-based with Solid-style code

* docs(config): add solid offline example entry and update lockfile

* ci: apply automated fixes

* docs(react/guides/caching): unify staleTime wording to passive voice

* chore(pnpm-lock.yaml): fix broken lockfile for 'msw' dependency

* docs(solid/guides/caching): remove redundant 'StaleNote' override now unified with react

* docs(react/guides/caching): remove unused 'StaleNote' markers

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-02-26 14:27:51 +01:00

1.5 KiB

id title ref
query-functions Query Functions docs/framework/react/guides/query-functions.md
useQuery(() => ({ queryKey: ['todos'], queryFn: fetchAllTodos }))
useQuery(() => ({
  queryKey: ['todos', todoId],
  queryFn: () => fetchTodoById(todoId),
}))
useQuery(() => ({
  queryKey: ['todos', todoId],
  queryFn: async () => {
    const data = await fetchTodoById(todoId)
    return data
  },
}))
useQuery(() => ({
  queryKey: ['todos', todoId],
  queryFn: ({ queryKey }) => fetchTodoById(queryKey[1]),
}))
const todosQuery = useQuery(() => ({
  queryKey: ['todos', todoId],
  queryFn: async () => {
    if (somethingGoesWrong) {
      throw new Error('Oh no!')
    }
    if (somethingElseGoesWrong) {
      return Promise.reject(new Error('Oh no!'))
    }

    return data
  },
}))
useQuery(() => ({
  queryKey: ['todos', todoId],
  queryFn: async () => {
    const response = await fetch('/todos/' + todoId)
    if (!response.ok) {
      throw new Error('Network response was not ok')
    }
    return response.json()
  },
}))
function Todos(props) {
  const todosQuery = useQuery(() => ({
    queryKey: ['todos', { status: props.status, page: props.page }],
    queryFn: fetchTodoList,
  }))
}

// Access the key, status and page variables in your query function!
function fetchTodoList({ queryKey }) {
  const [_key, { status, page }] = queryKey
  return new Promise()
}