formik: Please make onSubmit/handleSubmit optional for use cases that don't involve submitting forms

🚀 Feature request

Current Behavior

The Formik component’s onSubmitprop is required. Failing to use it causes a runtime error when submitting and a TypeScript error (if you use it). You can work around this by passing a no-op function (onSubmit={() => {}}), but I think it’s misleading for beginners and makes it seem like you need some specific code inside onSubmit.

Desired Behavior

Formik should allow the onSubmit prop to be omitted. Similarly, withFormik’s handleSubmit option should be optional.

Suggested Solution

Make the type optional in TypeScript (using undefined or ?: in the prop type) and disable any submitting functionality when onSubmit is not given. Any validation and other synthetic events that don’t rely on submitting should still work.

Who does this impact? Who is this for?

Any form state management use case that doesn’t involve submitting forms, for example:

  • Beginners learning how to use Formik without a server
  • Experienced users creating a prototype of an app that doesn’t have a server implemented yet
  • Calculating results based on form fields immediately as the user changes/blurs fields (I have multiple calculator apps that do this, and it’s my main use case for this feature)
  • Persisting a bunch of fields to local storage without having to manage form state manually

Describe alternatives you’ve considered

There could be another library for managing state of form fields that aren’t for submitting, but personally I think Formik is good enough at that already (despite this issue) that it wouldn’t be worth the effort.

Additional context

When I first learned about Formik, I avoided it because I saw the onSubmit prop and assumed I had to have some sort of REST/GRAPHQL server/service in order to use it. I didn’t realize until later that I could just use it for local state management. I think making submit behavior optional would make it easier to learn and understand the purpose of Formik. For me the biggest advantage of Formik is managing form state, not submitting forms.

I’d be happy to try a pull request if there’s interest in merging it, otherwise I might just maintain a fork.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 36
  • Comments: 21 (8 by maintainers)

Commits related to this issue

Most upvoted comments

please fix this issue !

Please thumbs up and optionally subscribe to my thread if you want to support it without commenting on anything specific.

Also, would the maintainers be interested in PRs for this?

This is still an issue and the associated pull request is still active