Next.js Discord

Discord Forum

Prevent redirect on form submit

Answered
Rojoss posted this in #help-forum
Open in Discord
When I submit a form there is a redirect happening.
I'm trying to cancel the event but that's not working.
I just want to run some client side code when the form is submitted.

  const handleSubmit = async(e: FormEvent) => {
    e.preventDefault();
    console.log('form submitted')
  }


<form onSubmit={handleSubmit}>
<Button type='submit'>Save Changes</Button>
</form>
Answered by American Chinchilla
@Rojoss probably because the function is async
View full answer

5 Replies

American Chinchilla
@Rojoss probably because the function is async
Answer
An idea is simply to move that async into a seperate function
Alright thanks!
Havana
Just use 'react-hook-form' instead of going full vanilla. It offers you more than just cancelling the event