Make handleSubmit return data as first parameter #10524
Unanswered
charlestbell
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi Bill,
Love your work on this package.
I'm writing because of a frustrating situation I keep running into. I use useFieldArray often, and as you'd expect, I'm working with dynamic forms and data. So because of that, I often have to pass in the thing I clicked on into the submit handler. Something like this:
then in the onSubmit, I can get the item out, and then the onSubmit knows what was clicked on
This works ok, but problems start to arise if I need to pass multiple items in, especially with variable amounts of arguments. Because handleSubmit always puts the form data as the last parameter, so what was once the data argument, isn't anymore, and everything breaks.
I tried using object instead
But this isn't a full solution, because if I send in no arguments like this:
Then it all breaks again because the position of the data parameter sent to onSubmit changes.
My suggestion:
2nd Approach
A Second approach would be to have handleSubmit take an object, instead of regular arguments. Something like
Then inside of handleSumbit, it would pass all of the extra arguments (myData1, myData2, etc) on to the onSubmit
And in this way, we also get to pass in a variable number of arguments with no worries.
And note, most of the time the handleSubmit call would still be very clean, so long as their onSubmit, and onError are named the same as the key.
The only thing added is some brackets {}
What do you think?
Beta Was this translation helpful? Give feedback.
All reactions