Reusable app hooks #1179
Mergedcrutchcorn(GitHub profile, opens in a new tab)merged 23 commits into
main fromrefs/pull/1179/headmerged by crutchcorn(GitHub profile, opens in a new tab) LeCarbonator(GitHub profile, opens in a new tab) requested changes
@@ -0,0 +11,21 @@11+12+The most powerful way to compose forms is to create custom form hooks. This allows you to create a form hook that is tailored to your application's needs, including pre-bound custom UI components and more.13+14+At it's most basic, `createFormHook` is a function that takes a `fieldContext` and `formContext` and returns a `useAppForm` hook.15+16+ This un-customized `useAppForm` hook is identical to `useForm`, but that will quickly change as we add more options to `createFormHook`.17+18+```tsx19+import {createFormHookContexts, createFormHook } from '@tanstack/react-form'20+21+const { fieldContext, formContext } =22+ createFormHookContexts()23+24+const { useAppForm } = createFormHook({25+ fieldContext,26+ formContext,27+ // We'll learn more about these options later28+ fieldComponents: {},29+ formComponents: {},30+})31+@@ -0,0 +32,21 @@32+function App() {33+ const form = useAppForm({34+ // Supports all useForm options35+ defaultValues: {36+ firstName: 'John',37+ lastName: 'Doe',38+ },39+ })40+41+ return <form.Field /> // ...42+}43+```44+45+## Pre-bound Field Components46+47+Once this scaffolding is in place, you can start adding custom field and form components to your form hook.48+49+```tsx50+function TextField({ label }: { label: string }) {51+ // Use the context returned from `createFormHookContexts`52+ // The `Field` that it should have a `value` type of `string`@@ -0,0 +125,21 @@125+126+const { useAppForm, withForm } = createFormHook({127+ fieldComponents: {},128+ formComponents: {129+ SubscribeButton,130+ },131+ fieldContext,132+ formContext,133+})134+135+function App() {136+ const form = useAppForm({137+ defaultValues: {138+ firstName: 'John',139+ lastName: 'Doe',140+ },141+ })142+143+ return (144+ <form.AppForm>145+ <form.SubscribeButton label="Submit" />@@ -0,0 +133,21 @@133+})134+135+function App() {136+ const form = useAppForm({137+ defaultValues: {138+ firstName: 'John',139+ lastName: 'Doe',140+ },141+ })142+143+ return (144+ <form.AppForm>145+ <form.SubscribeButton label="Submit" />146+ </form.AppForm>147+ )148+}149+```150+151+# Breaking big forms into smaller pieces152+153+Sometimes forms get very large; it's just how it goes sometimes. While TanStack Form supports large forms well, it's never fun to work with a files hundreds or thousands of lines of code long.@@ -0,0 +154,21 @@154+155+To solve this, we support breaking forms into smaller pieces using the `withForm` higher-order component.156+157+```tsx158+const { useAppForm, withForm } = createFormHook({159+ fieldComponents: {160+ TextField,161+ },162+ formComponents: {163+ SubscribeButton,164+ },165+ fieldContext,166+ formContext,167+})168+169+const ChildForm = withForm({170+ // These valeus are only used for type-checking, and are not used at runtime171+ // This allows you to `...formOpts` from `formOptions` without needing to redeclare the options172+ defaultValues: {173+ firstName: 'John',174+ lastName: 'Doe',crutchcorn(GitHub profile, opens in a new tab) changed the title
crutchcorn(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
View your CI Pipeline Execution ↗ for commit 7d9300e6af58a8bcfb9492fe9fa3869d84ce2629.
nx affected --targets=test:sherif,test:knip,tes...nx run-many --target=build --exclude=examples/**☁️ Nx Cloud last updated this comment at
2025-03-01 06:04:33UTC