Best way to handle multiple form submit buttons #12656
-
I have a form that has multiple (two or more) submit buttons that each have different behavior. I'm not sure what the best way to determine which button was clicked in the form onSubmit handler is. I thought of using a useState variable that gets set by each button's onClick handler, but I wasn't sure if there is a better way in react-hook-form to handle this. Any ideas? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
If each button has it's own separate submission logic, you can use both functions separately with If you want to be able to use the default form element submission behaviour, (by hitting Hard to give a more concrete suggestions without being able to see any of the code though. If you were able to create a code sandbox to demonstrate your issue that would make it easier to help. |
Beta Was this translation helpful? Give feedback.
It seems from your code snippet that submitting the form by pressing "Enter" would always submit the form via
onSubmitFunctionOne
(or whatever the default value for the useState hook is set to).In that case it would be the same as having:
If each button is only linked to one function, then you would know/be able to infer which button was clicked by which function was called, right?