TanStack Form 常被批评之处在于其开箱即用的冗长。虽然这可能有助于教学——帮助巩固对 API 的理解——但在生产用例中并非理想选择。
因此,虽然 form.Field 提供了 TanStack Form 最强大、最灵活的用法,但我们提供了封装它的 API,使您的应用程序代码更简洁。
组合表单的最强大方式是创建自定义表单 Hook。这允许您创建一个根据应用程序需求量身定制的表单 Hook,包括预绑定的自定义 UI 组件等。
最基础的 createFormHook 是一个函数,它接收一个 fieldContext 和 formContext,并返回一个 useAppForm Hook。
这个未定制的 useAppForm Hook 与 useForm 相同,但随着我们将更多选项添加到 createFormHook 中,这种情况将很快改变。
import { createFormHookContexts, createFormHook } from '@tanstack/solid-form'
// export useFieldContext for use in your custom components
export const { fieldContext, formContext, useFieldContext } =
createFormHookContexts()
const { useAppForm } = createFormHook({
fieldContext,
formContext,
// We'll learn more about these options later
fieldComponents: {},
formComponents: {},
})
function App() {
const form = useAppForm(() => ({
// Supports all useForm options
defaultValues: {
firstName: 'John',
lastName: 'Doe',
},
}))
return <form.Field /> // ...
}
import { createFormHookContexts, createFormHook } from '@tanstack/solid-form'
// export useFieldContext for use in your custom components
export const { fieldContext, formContext, useFieldContext } =
createFormHookContexts()
const { useAppForm } = createFormHook({
fieldContext,
formContext,
// We'll learn more about these options later
fieldComponents: {},
formComponents: {},
})
function App() {
const form = useAppForm(() => ({
// Supports all useForm options
defaultValues: {
firstName: 'John',
lastName: 'Doe',
},
}))
return <form.Field /> // ...
}
一旦有了这个脚手架,您就可以开始向表单 Hook 中添加自定义字段和表单组件了。
注意: useFieldContext 必须与从自定义表单上下文中导出的那个相同。
import { useFieldContext } from './form-context.tsx'
export function TextField(props: { label: string }) {
// The `Field` infers that it should have a `value` type of `string`
const field = useFieldContext<string>()
return (
<label>
<div>{props.label}</div>
<input
value={field().state.value}
onChange={(e) => field().handleChange(e.target.value)}
/>
</label>
)
}
import { useFieldContext } from './form-context.tsx'
export function TextField(props: { label: string }) {
// The `Field` infers that it should have a `value` type of `string`
const field = useFieldContext<string>()
return (
<label>
<div>{props.label}</div>
<input
value={field().state.value}
onChange={(e) => field().handleChange(e.target.value)}
/>
</label>
)
}
然后,您就可以将此组件注册到您的表单 Hook 中。
import { TextField } from './text-field.tsx'
const { useAppForm } = createFormHook({
fieldContext,
formContext,
fieldComponents: {
TextField,
},
formComponents: {},
})
import { TextField } from './text-field.tsx'
const { useAppForm } = createFormHook({
fieldContext,
formContext,
fieldComponents: {
TextField,
},
formComponents: {},
})
并在您的表单中使用它
function App() {
const form = useAppForm(() => ({
defaultValues: {
firstName: 'John',
lastName: 'Doe',
},
}))
return (
// Notice the `AppField` instead of `Field`; `AppField` provides the required context
<form.AppField
name="firstName"
children={(field) => <field.TextField label="First Name" />}
/>
)
}
function App() {
const form = useAppForm(() => ({
defaultValues: {
firstName: 'John',
lastName: 'Doe',
},
}))
return (
// Notice the `AppField` instead of `Field`; `AppField` provides the required context
<form.AppField
name="firstName"
children={(field) => <field.TextField label="First Name" />}
/>
)
}
这不仅可以重用共享组件的 UI,还可以保留您期望从 TanStack Form 中获得的类型安全:输入错误 name 会得到一个 TypeScript 错误。
虽然 form.AppField 解决了 Field 样板和可重用性方面的许多问题,但它并没有解决表单样板和可重用性的问题。
特别是,能够共享 form.Subscribe 的实例,例如用于响应式的表单提交按钮,是一个常见的用例。
function SubscribeButton(props: { label: string }) {
const form = useFormContext()
return (
<form.Subscribe selector={(state) => state.isSubmitting}>
{(isSubmitting) => (
<button type="submit" disabled={isSubmitting()}>
{props.label}
</button>
)}
</form.Subscribe>
)
}
const { useAppForm, withForm } = createFormHook({
fieldComponents: {},
formComponents: {
SubscribeButton,
},
fieldContext,
formContext,
})
function App() {
const form = useAppForm(() => ({
defaultValues: {
firstName: 'John',
lastName: 'Doe',
},
}))
return (
<form.AppForm>
// Notice the `AppForm` component wrapper; `AppForm` provides the required
context
<form.SubscribeButton label="Submit" />
</form.AppForm>
)
}
function SubscribeButton(props: { label: string }) {
const form = useFormContext()
return (
<form.Subscribe selector={(state) => state.isSubmitting}>
{(isSubmitting) => (
<button type="submit" disabled={isSubmitting()}>
{props.label}
</button>
)}
</form.Subscribe>
)
}
const { useAppForm, withForm } = createFormHook({
fieldComponents: {},
formComponents: {
SubscribeButton,
},
fieldContext,
formContext,
})
function App() {
const form = useAppForm(() => ({
defaultValues: {
firstName: 'John',
lastName: 'Doe',
},
}))
return (
<form.AppForm>
// Notice the `AppForm` component wrapper; `AppForm` provides the required
context
<form.SubscribeButton label="Submit" />
</form.AppForm>
)
}
有时表单会变得非常大;有时就是这样。虽然 TanStack Form 可以很好地支持大型表单,但处理成百上千行代码的文件从来都不是一件有趣的事。
为了解决这个问题,我们支持使用 withForm 高阶组件将表单拆分成更小的部分。
const { useAppForm, withForm } = createFormHook({
fieldComponents: {
TextField,
},
formComponents: {
SubscribeButton,
},
fieldContext,
formContext,
})
const ChildForm = withForm({
// These values are only used for type-checking, and are not used at runtime
// This allows you to `...formOpts` from `formOptions` without needing to redeclare the options
defaultValues: {
firstName: 'John',
lastName: 'Doe',
},
// Optional, but adds props to the `render` function in addition to `form`
props: {
// These props are also set as default values for the `render` function
title: 'Child Form',
},
render: function Render(props) {
return (
<div>
<p>{props.title}</p>
<props.form.AppField
name="firstName"
children={(field) => <field.TextField label="First Name" />}
/>
<props.form.AppForm>
<props.form.SubscribeButton label="Submit" />
</props.form.AppForm>
</div>
)
},
})
function App() {
const form = useAppForm(() => ({
defaultValues: {
firstName: 'John',
lastName: 'Doe',
},
}))
return <ChildForm form={form} title={'Testing'} />
}
const { useAppForm, withForm } = createFormHook({
fieldComponents: {
TextField,
},
formComponents: {
SubscribeButton,
},
fieldContext,
formContext,
})
const ChildForm = withForm({
// These values are only used for type-checking, and are not used at runtime
// This allows you to `...formOpts` from `formOptions` without needing to redeclare the options
defaultValues: {
firstName: 'John',
lastName: 'Doe',
},
// Optional, but adds props to the `render` function in addition to `form`
props: {
// These props are also set as default values for the `render` function
title: 'Child Form',
},
render: function Render(props) {
return (
<div>
<p>{props.title}</p>
<props.form.AppField
name="firstName"
children={(field) => <field.TextField label="First Name" />}
/>
<props.form.AppForm>
<props.form.SubscribeButton label="Submit" />
</props.form.AppForm>
</div>
)
},
})
function App() {
const form = useAppForm(() => ({
defaultValues: {
firstName: 'John',
lastName: 'Doe',
},
}))
return <ChildForm form={form} title={'Testing'} />
}
为什么是高阶组件而不是 Hook?
虽然 Hook 是 Solid 的未来,但高阶组件仍然是组合的强大工具。特别是 withForm 的 API 使我们能够在不要求用户传递泛型的情况下实现强大的类型安全。
虽然上面的示例对于入门非常有用,但它们对于某些可能包含数百个表单和字段组件的用例来说并不是理想的。特别是,您可能不希望将所有表单和字段组件都包含在使用您的表单 Hook 的每个文件的包中。
为了解决这个问题,您可以将 createFormHook TanStack API 与 Solid 的 lazy 和 Suspense 组件结合使用。
// src/hooks/form-context.ts
import { createFormHookContexts } from '@tanstack/solid-form'
export const { fieldContext, useFieldContext, formContext, useFormContext } =
createFormHookContexts()
// src/hooks/form-context.ts
import { createFormHookContexts } from '@tanstack/solid-form'
export const { fieldContext, useFieldContext, formContext, useFormContext } =
createFormHookContexts()
// src/components/text-field.tsx
import { useFieldContext } from '../hooks/form-context.tsx'
export default function TextField(props: { label: string }) {
const field = useFieldContext<string>()
return (
<label>
<div>{props.label}</div>
<input
value={field().state.value}
onChange={(e) => field().handleChange(e.target.value)}
/>
</label>
)
}
// src/components/text-field.tsx
import { useFieldContext } from '../hooks/form-context.tsx'
export default function TextField(props: { label: string }) {
const field = useFieldContext<string>()
return (
<label>
<div>{props.label}</div>
<input
value={field().state.value}
onChange={(e) => field().handleChange(e.target.value)}
/>
</label>
)
}
// src/hooks/form.ts
import { lazy } from 'solid-js'
import { createFormHook } from '@tanstack/solid-form'
const TextField = lazy(() => import('../components/text-fields.tsx'))
const { useAppForm, withForm } = createFormHook({
fieldContext,
formContext,
fieldComponents: {
TextField,
},
formComponents: {},
})
// src/hooks/form.ts
import { lazy } from 'solid-js'
import { createFormHook } from '@tanstack/solid-form'
const TextField = lazy(() => import('../components/text-fields.tsx'))
const { useAppForm, withForm } = createFormHook({
fieldContext,
formContext,
fieldComponents: {
TextField,
},
formComponents: {},
})
// src/App.tsx
import { Suspense } from 'solid-js'
import { PeoplePage } from './features/people/form.tsx'
export default function App() {
return (
<Suspense fallback={<p>Loading...</p>}>
<PeoplePage />
</Suspense>
)
}
// src/App.tsx
import { Suspense } from 'solid-js'
import { PeoplePage } from './features/people/form.tsx'
export default function App() {
return (
<Suspense fallback={<p>Loading...</p>}>
<PeoplePage />
</Suspense>
)
}
这将显示 Suspense 的回退 UI,同时加载 TextField 组件,然后在加载完成后渲染表单。
现在我们已经涵盖了创建自定义表单 Hook 的基础知识,让我们在一个示例中将它们整合在一起。
// /src/hooks/form.ts, to be used across the entire app
const { fieldContext, useFieldContext, formContext, useFormContext } =
createFormHookContexts()
function TextField(props: { label: string }) {
const field = useFieldContext<string>()
return (
<label>
<div>{props.label}</div>
<input
value={field().state.value}
onChange={(e) => field().handleChange(e.target.value)}
/>
</label>
)
}
function SubscribeButton(props: { label: string }) {
const form = useFormContext()
return (
<form.Subscribe selector={(state) => state.isSubmitting}>
{(isSubmitting) => (
<button disabled={isSubmitting()}>{props.label}</button>
)}
</form.Subscribe>
)
}
const { useAppForm, withForm } = createFormHook({
fieldComponents: {
TextField,
},
formComponents: {
SubscribeButton,
},
fieldContext,
formContext,
})
// /src/features/people/shared-form.ts, to be used across `people` features
const formOpts = formOptions({
defaultValues: {
firstName: 'John',
lastName: 'Doe',
},
})
// /src/features/people/nested-form.ts, to be used in the `people` page
const ChildForm = withForm({
...formOpts,
// Optional, but adds props to the `render` function outside of `form`
props: {
title: 'Child Form',
},
render: (props) => {
return (
<div>
<p>{title}</p>
<props.form.AppField
name="firstName"
children={(field) => <field.TextField label="First Name" />}
/>
<props.form.AppForm>
<props.form.SubscribeButton label="Submit" />
</props.form.AppForm>
</div>
)
},
})
// /src/features/people/page.ts
const Parent = () => {
const form = useAppForm({
...formOpts,
})
return <ChildForm form={form} title={'Testing'} />
}
// /src/hooks/form.ts, to be used across the entire app
const { fieldContext, useFieldContext, formContext, useFormContext } =
createFormHookContexts()
function TextField(props: { label: string }) {
const field = useFieldContext<string>()
return (
<label>
<div>{props.label}</div>
<input
value={field().state.value}
onChange={(e) => field().handleChange(e.target.value)}
/>
</label>
)
}
function SubscribeButton(props: { label: string }) {
const form = useFormContext()
return (
<form.Subscribe selector={(state) => state.isSubmitting}>
{(isSubmitting) => (
<button disabled={isSubmitting()}>{props.label}</button>
)}
</form.Subscribe>
)
}
const { useAppForm, withForm } = createFormHook({
fieldComponents: {
TextField,
},
formComponents: {
SubscribeButton,
},
fieldContext,
formContext,
})
// /src/features/people/shared-form.ts, to be used across `people` features
const formOpts = formOptions({
defaultValues: {
firstName: 'John',
lastName: 'Doe',
},
})
// /src/features/people/nested-form.ts, to be used in the `people` page
const ChildForm = withForm({
...formOpts,
// Optional, but adds props to the `render` function outside of `form`
props: {
title: 'Child Form',
},
render: (props) => {
return (
<div>
<p>{title}</p>
<props.form.AppField
name="firstName"
children={(field) => <field.TextField label="First Name" />}
/>
<props.form.AppForm>
<props.form.SubscribeButton label="Submit" />
</props.form.AppForm>
</div>
)
},
})
// /src/features/people/page.ts
const Parent = () => {
const form = useAppForm({
...formOpts,
})
return <ChildForm form={form} title={'Testing'} />
}
这里有一个图表,可以帮助您决定应该使用哪些 API。
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。