Open History to see the last commit
1import { TanStackDevtools } from '@tanstack/react-devtools'
2import { useForm } from '@tanstack/react-form'
3import { formDevtoolsPlugin } from '@tanstack/react-form-devtools'
4import ReactDOM from 'react-dom/client'
5import './index.css'
6import { StringField } from './StringField'
7import { sharedFormOptions } from './sharedForm'
8import { FormSection } from './FormSection'
9import { FormSubmitButton } from './FormSubmitButton'
10β
11function App() {
12 const form = useForm({
13 ...sharedFormOptions,
14 onSubmit: ({ value }) => {
15 // Do something with form data
16 console.log(value)
17 },
18 })
19β
20 return (
21 <div>
22 <h1>Simple Form Example</h1>
23 <form
24 onSubmit={(e) => {
25 e.preventDefault()
26 e.stopPropagation()
27 form.handleSubmit()
28 }}
29 >
30 <form.Field name="firstName">
31 {(field) => <StringField field={field} />}
32 </form.Field>
33 <form.Field name="lastName">
34 {(field) => <StringField field={field} />}
35 </form.Field>
36β
37 <FormSection form={form} />
38β
39 <FormSubmitButton form={form} />
40 <button
41 type="reset"
42 onClick={(e) => {
43 // Avoid unexpected resets of form elements (especially <select> elements)
44 e.preventDefault()
45 form.reset()
46 }}
47 >
48 Reset
49 </button>
50 </form>
51 <TanStackDevtools
52 plugins={[formDevtoolsPlugin()]}
53 config={{
54 hideUntilHover: false,
55 }}
56 />
57 </div>
58 )
59}
60β
61ReactDOM.createRoot(document.getElementById('root')!).render(<App />)