formOpts.onSubmit example
Does anyone have an example of how to move their onSubmit() to the formOpts? I would need to pass quite a bit of state and I can't figure it out. It would be really nice if I could set new state for tanstack store for postError to use in the onSubmit as well so I could then use it in A form component without passing it as props.
11 Replies
eager-peach•9mo ago
works just fine on the Form Composition example if it is not overridden on the form itself.
... I think we need some more information or code to get the problem. Can you write up some code that shows what you're trying to achieve?
stormy-goldOP•9mo ago
I almost got it. I just don't understand how to use form.reset() with the onSubmit declared outside of the form.
eager-peach•9mo ago
onSubmit also has a formApi: AnyFormApi prop.
I haven't tested it, but this should work? Typescript certainly likes it.
stormy-goldOP•9mo ago
Thank you so much, I was looking everywhere except there, lol. Thanks again!!
eager-peach•9mo ago
No problem, I was about to suggest passing in the form with that type (which would have been super weird) when I got the idea to look at what is actually passed besides value 😄
rival-black•9mo ago
this actually made me think, could TSF also export a higher order hook that contains the form as an arg in it, like we have with withForm for higher order components
stormy-goldOP•9mo ago
I think Crutchcorn said somewhere he'd leave that up to us if we wanted to put it in a context. I have to pass the defaultValues everywhere because I generate them from a CMS. I wish I could generate them right in the withForm(), but instead I have to wrap it in a React Component, and also pass the form as any, but it's not too bad.
eager-peach•9mo ago
Why not
AnyFormApi to at least make it a form type?stormy-goldOP•9mo ago
I knew you had a Type I could use. Thanks again!
AnyFormApi actually causes a type error when passing it into a withForm component. But AnyFormApi | any works and gives me some form auto complete.eager-peach•9mo ago
Dang it, I didn't think that would bite even when any is involved as well 😅 .
stormy-goldOP•9mo ago
This solved my problem with needing to pass the form other components.