-
-
Notifications
You must be signed in to change notification settings - Fork 607
Add preact integration for tanstack form #2043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JoviDeCroock
wants to merge
15
commits into
TanStack:main
Choose a base branch
from
JoviDeCroock:preact-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c104cd7
Add preact integration for tanstack form
JoviDeCroock 13142b3
ci: apply automated fixes and generate docs
autofix-ci[bot] f854bfa
Remove accidental reference to compat
JoviDeCroock ccc4c6f
Remove use clien
JoviDeCroock 5ec0c52
onInput for test parity
JoviDeCroock 2e80b40
Add docs
JoviDeCroock a8dc0ee
Remove useUUid
JoviDeCroock 22decc1
ci: apply automated fixes and generate docs
autofix-ci[bot] aa83641
Add example
JoviDeCroock 203d01d
Fix
JoviDeCroock faa42d9
chore: remove CHANGELOG
crutchcorn b8189a9
chore: update version
crutchcorn e739893
ci: apply automated fixes and generate docs
autofix-ci[bot] 0e39f14
Create compat bridge for outdated form-core store
JoviDeCroock c223009
Apply suggestion from @JoviDeCroock
JoviDeCroock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| --- | ||
| id: arrays | ||
| title: Arrays | ||
| --- | ||
|
|
||
| TanStack Form supports arrays as values in a form, including sub-object values inside of an array. | ||
|
|
||
| ## Basic Usage | ||
|
|
||
| To use an array, you can use `field.state.value` on an array value: | ||
|
|
||
| ```jsx | ||
| function App() { | ||
| const form = useForm({ | ||
| defaultValues: { | ||
| people: [], | ||
| }, | ||
| }) | ||
|
|
||
| return ( | ||
| <form.Field name="people" mode="array"> | ||
| {(field) => ( | ||
| <div> | ||
| {field.state.value.map((_, i) => { | ||
| // ... | ||
| })} | ||
| </div> | ||
| )} | ||
| </form.Field> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| This will generate the mapped JSX every time you run `pushValue` on `field`: | ||
|
|
||
| ```jsx | ||
| <button onClick={() => field.pushValue({ name: '', age: 0 })} type="button"> | ||
| Add person | ||
| </button> | ||
| ``` | ||
|
|
||
| Finally, you can use a subfield like so: | ||
|
|
||
| ```jsx | ||
| <form.Field key={i} name={`people[${i}].name`}> | ||
| {(subField) => ( | ||
| <input | ||
| value={subField.state.value} | ||
| onInput={(e) => subField.handleChange(e.target.value)} | ||
| /> | ||
| )} | ||
| </form.Field> | ||
| ``` | ||
|
|
||
| ## Full Example | ||
|
|
||
| ```jsx | ||
| function App() { | ||
| const form = useForm({ | ||
| defaultValues: { | ||
| people: [], | ||
| }, | ||
| onSubmit({ value }) { | ||
| alert(JSON.stringify(value)) | ||
| }, | ||
| }) | ||
|
|
||
| return ( | ||
| <div> | ||
| <form | ||
| onSubmit={(e) => { | ||
| e.preventDefault() | ||
| e.stopPropagation() | ||
| form.handleSubmit() | ||
| }} | ||
| > | ||
| <form.Field name="people" mode="array"> | ||
| {(field) => { | ||
| return ( | ||
| <div> | ||
| {field.state.value.map((_, i) => { | ||
| return ( | ||
| <form.Field key={i} name={`people[${i}].name`}> | ||
| {(subField) => { | ||
| return ( | ||
| <div> | ||
| <label> | ||
| <div>Name for person {i}</div> | ||
| <input | ||
| value={subField.state.value} | ||
| onInput={(e) => | ||
| subField.handleChange(e.target.value) | ||
| } | ||
| /> | ||
| </label> | ||
| </div> | ||
| ) | ||
| }} | ||
| </form.Field> | ||
| ) | ||
| })} | ||
| <button | ||
| onClick={() => field.pushValue({ name: '', age: 0 })} | ||
| type="button" | ||
| > | ||
| Add person | ||
| </button> | ||
| </div> | ||
| ) | ||
| }} | ||
| </form.Field> | ||
| <form.Subscribe | ||
| selector={(state) => [state.canSubmit, state.isSubmitting]} | ||
| children={([canSubmit, isSubmitting]) => ( | ||
| <button type="submit" disabled={!canSubmit}> | ||
| {isSubmitting ? '...' : 'Submit'} | ||
| </button> | ||
| )} | ||
| /> | ||
| </form> | ||
| </div> | ||
| ) | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| --- | ||
| id: async-initial-values | ||
| title: Async Initial Values | ||
| --- | ||
|
|
||
| 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](https://tanstack.com/query). | ||
|
|
||
| As such, this guide shows you how you can mix-n-match TanStack Form with TanStack Query to achieve the desired behavior. | ||
|
|
||
| ## Basic Usage | ||
|
|
||
| ```tsx | ||
| import { useForm } from '@tanstack/preact-form' | ||
| import { useQuery } from '@tanstack/react-query' | ||
|
|
||
| export default function App() { | ||
| const {data, isLoading} = useQuery({ | ||
| queryKey: ['data'], | ||
| queryFn: async () => { | ||
| await new Promise((resolve) => setTimeout(resolve, 1000)) | ||
| return {firstName: 'FirstName', lastName: "LastName"} | ||
| } | ||
| }) | ||
|
|
||
| const form = useForm({ | ||
| 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 ( | ||
| // ... | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| 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. | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: TanStack/form
Length of output: 161
🏁 Script executed:
Repository: TanStack/form
Length of output: 1548
🏁 Script executed:
Repository: TanStack/form
Length of output: 690
🏁 Script executed:
Repository: TanStack/form
Length of output: 403
🏁 Script executed:
Repository: TanStack/form
Length of output: 2127
🌐 Web query:
@tanstack/preact-query npm package💡 Result:
@tanstack/preact-queryis the Preact adapter for TanStack Query (server-state fetching/caching/synchronization). It provides Preact-friendly exports likeQueryClient,QueryClientProvider, and hooks such asuseQuery,useMutation, etc. [3][4]Install
[1]
Basic usage
[2][3]
Latest version (as of Mar 31, 2026):
5.95.2[5]Docs: TanStack Query → Preact section (installation/quick start/guides). [1][2]
References: [1] (tanstack.com) [2] (tanstack.com) [3] (tanstack.com) [4] (tanstack.com) [5] (newreleases.io)
Citations:
Use
@tanstack/preact-queryinstead of@tanstack/react-query.Line 21 imports from
@tanstack/react-query, which is mismatched for this Preact guide. The correct package is@tanstack/preact-query, consistent with how other frameworks (React, Vue, Solid) each have their own TanStack Query adapter.Suggested diff
import { useForm } from '@tanstack/preact-form' -import { useQuery } from '@tanstack/react-query' +import { useQuery } from '@tanstack/preact-query'📝 Committable suggestion
🤖 Prompt for AI Agents