T
TanStack4w ago
unwilling-turquoise

reset after form success

using nextjs server actions and tanstack form, how can i reset the form if the request succeeds ?
1 Reply
fascinating-indigo
fascinating-indigo2w ago
There is formApi available in onSubmit callback inside useForm hook. Not sure how exactly NextJS works as you didn't provided any example but this is how you can reset form using useForm hook:
const form = useForm({
defaultValues: {
emailAddress: "",
},
onSubmit: async ({ value, formApi }) => {
await doYourAsyncWork(value);

// reset the API
formApi.reset();

// here you can invalidate your router/navigate somewhere else etc
},
});
const form = useForm({
defaultValues: {
emailAddress: "",
},
onSubmit: async ({ value, formApi }) => {
await doYourAsyncWork(value);

// reset the API
formApi.reset();

// here you can invalidate your router/navigate somewhere else etc
},
});

Did you find this page helpful?