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/initial-query-data.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

3 KiB

id title ref replace
initial-query-data Initial Query Data docs/framework/react/guides/initial-query-data.md
@tanstack/react-query useMutationState[(] useMutation[(] useQuery[(] useQueries[(] useInfiniteQuery[(]
@tanstack/solid-query useMutationState(() => useMutation(() => useQuery(() => useQueries(() => useInfiniteQuery(() =>
const todosQuery = useQuery(() => ({
  queryKey: ['todos'],
  queryFn: () => fetch('/todos'),
  initialData: initialTodos,
}))
// Will show initialTodos immediately, but also immediately refetch todos after mount
const todosQuery = useQuery(() => ({
  queryKey: ['todos'],
  queryFn: () => fetch('/todos'),
  initialData: initialTodos,
}))
// Show initialTodos immediately, but won't refetch until another interaction event is encountered after 1000 ms
const todosQuery = useQuery(() => ({
  queryKey: ['todos'],
  queryFn: () => fetch('/todos'),
  initialData: initialTodos,
  staleTime: 1000,
}))
// Show initialTodos immediately, but won't refetch until another interaction event is encountered after 1000 ms
const todosQuery = useQuery(() => ({
  queryKey: ['todos'],
  queryFn: () => fetch('/todos'),
  initialData: initialTodos,
  staleTime: 60 * 1000, // 1 minute
  // This could be 10 seconds ago or 10 minutes ago
  initialDataUpdatedAt: initialTodosUpdatedTimestamp, // eg. 1608412420052
}))
const todosQuery = useQuery(() => ({
  queryKey: ['todos'],
  queryFn: () => fetch('/todos'),
  initialData: () => getExpensiveTodos(),
}))
const todoQuery = useQuery(() => ({
  queryKey: ['todo', todoId],
  queryFn: () => fetch('/todos'),
  initialData: () => {
    // Use a todo from the 'todos' query as the initial data for this todo query
    return queryClient.getQueryData(['todos'])?.find((d) => d.id === todoId)
  },
}))
const todoQuery = useQuery(() => ({
  queryKey: ['todos', todoId],
  queryFn: () => fetch(`/todos/${todoId}`),
  initialData: () =>
    queryClient.getQueryData(['todos'])?.find((d) => d.id === todoId),
  initialDataUpdatedAt: () =>
    queryClient.getQueryState(['todos'])?.dataUpdatedAt,
}))
const todoQuery = useQuery(() => ({
  queryKey: ['todo', todoId],
  queryFn: () => fetch(`/todos/${todoId}`),
  initialData: () => {
    // Get the query state
    const state = queryClient.getQueryState(['todos'])

    // If the query exists and has data that is no older than 10 seconds...
    if (state && Date.now() - state.dataUpdatedAt <= 10 * 1000) {
      // return the individual todo
      return state.data.find((d) => d.id === todoId)
    }

    // Otherwise, return undefined and let it fetch from a hard loading state!
  },
}))