Next.js Discord

Discord Forum

Calling form action, when onSubmit calls preventDefault

Answered
Spinge Bib Sqorpnts posted this in #help-forum
Open in Discord
Hello! I have a form where I must call preventDefault inside onSubmit, so that a select element I have in the form would not reset. But it seems that calling preventDefault also prevents the action from running.

Calling event.currentTarget.requestSubmit() does not seem to help either.
Answered by Coffee Coke
actions are used to call server components, if samething can be done on use client, you should move it to the onsubmit
View full answer

11 Replies

could you show me your select element? @Spinge Bib Sqorpnts
@Coffee Coke could you show me your select element? <@332567411620577280>
It is an MUI Select element, I'll send something in a moment
putting e.preventDefault() inside the form onsubmit didn't work?
it does stop the select from being cleared, but then action is not called :/
why call action in the first place? do it in the onsubmit callback
@Coffee Coke why call action in the first place? do it in the onsubmit callback
Is there any real difference between onSubmit and action?
the action itself is not use server, but does call an external function that has use server
@Coffee Coke action requires form to behave normally (without preventDefault) so you can't have both
That I understood, but there should not be any difference if I simply move the code from action to onSubmit, right?
if don't need to use "use server" then sure
actions are used to call server components, if samething can be done on use client, you should move it to the onsubmit
Answer