File: async-initial-values.md | Updated: 11/15/2025
Search...
+ K
Auto
Docs Examples GitHub Contributors
Docs Examples GitHub Contributors
Docs Examples GitHub Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Maintainers Partners Support Learn StatsBETA Discord Merch Blog GitHub Ethos Brand Guide
Documentation
Framework
Solid
Version
Latest
Search...
+ K
Menu
Getting Started
Guides
API Reference
Examples
Framework
Solid
Version
Latest
Menu
Getting Started
Guides
API Reference
Examples
On this page
Copy Markdown
Let's say that you want to fetch some data from an API and use it as the initial value of a form.
While this problem sounds simple on the surface, there are hidden complexities you might not have thought of thus far.
For example, you might want to show a loading spinner while the data is being fetched, or you might want to handle errors gracefully. Likewise, you could also find yourself looking for a way to cache the data so that you don't have to fetch it every time the form is rendered.
While we could implement many of these features from scratch, it would end up looking a lot like another project we maintain: TanStack Query .
As such, this guide shows you how you can mix-n-match TanStack Form with TanStack Query to achieve the desired behavior.
tsx
import { createForm } from '@tanstack/solid-form'
import { createQuery } from '@tanstack/solid-query'
export default function App() {
const { data, isLoading } = createQuery(() => ({
queryKey: ['data'],
queryFn: async () => {
await new Promise((resolve) => setTimeout(resolve, 1000))
return { firstName: 'FirstName', lastName: 'LastName' }
},
}))
const form = createForm(() => ({
defaultValues: {
firstName: data?.firstName ?? '',
lastName: data?.lastName ?? '',
},
onSubmit: async ({ value }) => {
// Do something with form data
console.log(value)
},
}))
if (isLoading) return <p>Loading..</p>
return null
}
import { createForm } from '@tanstack/solid-form'
import { createQuery } from '@tanstack/solid-query'
export default function App() {
const { data, isLoading } = createQuery(() => ({
queryKey: ['data'],
queryFn: async () => {
await new Promise((resolve) => setTimeout(resolve, 1000))
return { firstName: 'FirstName', lastName: 'LastName' }
},
}))
const form = createForm(() => ({
defaultValues: {
firstName: data?.firstName ?? '',
lastName: data?.lastName ?? '',
},
onSubmit: async ({ value }) => {
// Do something with form data
console.log(value)
},
}))
if (isLoading) return <p>Loading..</p>
return null
}
This will show a loading spinner until the data is fetched, and then it will render the form with the fetched data as the initial values.
