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
dependent-tan•6mo 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?
conscious-sapphireOP•6mo ago
I almost got it. I just don't understand how to use form.reset() with the onSubmit declared outside of the form.
dependent-tan•6mo ago
onSubmit
also has a formApi: AnyFormApi
prop.
I haven't tested it, but this should work? Typescript certainly likes it.
conscious-sapphireOP•6mo ago
Thank you so much, I was looking everywhere except there, lol. Thanks again!!
dependent-tan•6mo 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 😄
rising-crimson•6mo 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
conscious-sapphireOP•6mo 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.
dependent-tan•6mo ago
Why not
AnyFormApi
to at least make it a form type?conscious-sapphireOP•6mo 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.dependent-tan•6mo ago
Dang it, I didn't think that would bite even when any is involved as well 😅 .
conscious-sapphireOP•6mo ago
This solved my problem with needing to pass the form other components.