Using next.js server actions, fields are set to undefined #2089
Send to my agentDescribe the bug#
When going from one route to another one that has a tanstack form, all fields are set to default value initially and then undefined right after. This causes an error on UI.
Assume 2 routes / and /form and the /form route has the tanstack form. The problem only happens (as far as I can tell) when:
- User goes to
/route. - User clicks a link on the page that redirects them to
/formroute - All fields on the form are set to their default values first then to undefined
If you refresh the page in the above example, or you directly go to /form route by entering the URL on your address bar and pressing enter, the problem doesn't occur. It appears that this only happens while routing the user from one page in the app to the /form page.
Your minimal, reproducible example#
https://codesandbox.io/p/devbox/pf2wkh
Steps to reproduce#
- Go to "/" route and open the console
- Click "Go to Form" link to go to "/form"
- Notice on the console prints:
email value default@gmail.com
password value 123456789
email value undefined
password value undefined
If you are running on strict mode you'll see logs twice due to double render
- If you refresh the page you'll notice that they are not set to undefined
Expected behavior#
Fields shouldn't be set to undefined
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
- OS: MacOS
- Browser: Edge
- 143.0.3650.96 (arm64)
TanStack Form adapter#
react-form
TanStack Form version#
1.28.5
TypeScript version#
^5
Additional context#
No response
The issue originates from the server and client state merging process.
const [state, action] = useActionState(someAction, initialFormState)initialFormStateobject hasvaluesproperty and it is undefined. When the following code runs to merge client and server state, I suppose it simply overrides the client state with server state. I didn't really check too deeply.const form = useForm({ ...formOpts, transform: useTransform((baseForm) => mergeForm(baseForm, state!), [state]), })One solution I found, is to augment this
initialFormStatewith the default values. For forms that have always the same default values, just useformOpts.defaultValuesconst [state, action] = useActionState(someAction, { ...initialFormState, values: formOpts.defaultValues }) const form = useForm({ ...formOpts, transform: useTransform((baseForm) => mergeForm(baseForm, state!), [state]), })If your default values are coming from database or some other source (for instance user settings forms), just take the default values as a prop in the component.
// FormInput -> z.infer<typeof formSchema> export const ClientComp = ({formData} : {formData: FormInput}) => { const [state, action] = useActionState(someAction, {...initialFormState, values: formData }) const form = useForm({ defaultValues: { ...formData }, transform: useTransform((baseForm) => mergeForm(baseForm, state!), [state]), }) // ... }Note that while using textarea, the error is a bit different however the root cause for the error is all the same.
fieldValidatorMeta?.lastAbortController.abort is not a functionWhile I found a way to get around this issue, I still think something must be done. Either updating the documentation or doing something about
initialFormState. It's very surprising to me that nobody else had an issue with this. It's practically impossible to not have this issue if you follow the official documentation for Next.js usage.I've started working on this. Will submit a PR soon.
LeCarbonator(GitHub profile, opens in a new tab) added labelsv1
LeCarbonator(GitHub profile, opens in a new tab) added labelsv2: needs investigation
Mishra-coder(GitHub profile, opens in a new tab) linked this to #2096
Sign in to join this conversation.
Hi! I’d like to work on this issue. Could you please assign it to me? I’ll make sure to follow the contribution guidelines. Thanks!