Skip to content
Source
Sign inSign up
TanStack/form

onSubmit meta docs #1183

Send to my agent
Closedcrutchcorn(GitHub profile, opens in a new tab) opened this issue 2 comments

Document the behavior of https://github.com/TanStack/form/pull/1167 in the React guides

Imagine a usecase where the user wants two buttons, they both send the item to the backend (after form validation) but one goes back to the list and the other goes to a different page, like a step 2 or a detail page.

const form = useForm({
    defaultValues: {
      firstName: '',
      lastName: '',
    },
    // Is 'never' by default unless you provide a type
    // {} is the default value passed to `onSubmit`'s `meta` property
    onSubmitMeta: {} as { someValue: string },
    onSubmit: async ({ value, meta }) => {
      // Do something with form data
      console.log(value)
      // Do something with the values passed via handleSubmit
      console.log(meta)
    },
  })

  return (
    <div>
      <h1>Simple Form Example</h1>
      <form
        onSubmit={(e) => {
          e.preventDefault()
          e.stopPropagation()
          form.handleSubmit({
            someValue: 'value',
          })
        }}
      >
  1. crutchcorn(GitHub profile, opens in a new tab) added labelstype: docs

  2. This feature is great for having 2 submit buttons that do slightly different behavior on submit, such as "Save and continue" & "Save and exit", right? Or is there something better for that use case?

Sign in to join this conversation.