How can i redirect after submit form React

Day Anuchit

i'm newbie for React maybe for english too. How can i redirect after form submitted ?

  function Test() {
  const { register, handleSubmit } = useForm()
  const onSubmit = data => {
    fetch(`http://localhost:4000/signup`)
    //Here i want to redirect after signup
  }
  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      ....
    </form>
  );
}
Jonathan Miles

fetch can be used with either callbacks or promises, you need to wait for the asynchronous request to finish before you redirect.

This is a simply callback example, it assumes you do not need to access the response body of the request or need to check the response status.

function Test() {
  const { register, handleSubmit } = useForm()
  const onSubmit = data => {
    fetch(`http://localhost:4000/signup`)
      .then(resp => {
        // Navigate here, either:
        // use browser (not nice if SPA)
        window.location = "http://www.url.com/path";
        // use connected react router
        // implementation specific
        // e.g. this.props.push("/path");
      });
  }
  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      ....
    </form>
  );
}

If you're familiar with Promises and async await, you could use the following

function Test() {
  const { register, handleSubmit } = useForm()
  const onSubmit = async (data) => {
    await fetch(`http://localhost:4000/signup`);
    // navigate here
  }
  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      ....
    </form>
  );
}

Ideally you should look to handle side effects like these via some kind of middleware, for example Redux Thunk, Promise, Sagas or Observables. This removes unnecessary business logic from your components, allows for cleaner testing and better separation of concerns.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

After submit the form how do I redirect to welcome component in React

How can I on Submit my Form redirect the Results to another Page in React

How can I test form submit in React?

How can I redirect to a form after an alert?

React+redux-form - redirect after submit

React router redirect after form submit

Redirect After Form Submit

Redirect after form submit or not?

How can I keep field values in a form after submit?

How can I empty the field of the form after submit?

how can I update my page after clicking the submit form?

Redirect after submit in React

How can I redirect after sending form to Salesforce?

After form submit redirect to domain

Redirect after form Submit (CSR)

Redirect to the cart after submit a form

Form - Redirect after hitting Submit

How can I conditionally redirect to a page after the axios response in react?

How can i submit a PHP form without redirect to action file? (send and get data with ajax)

react submit form then redirect issue

How to stop redirect from partial page after form submit

How to make redirect to new detail data after submit form on PHP

How to redirect the user to a new website after they submit the <form>?

How to redirect user after submitting form with React

How do I clear Bootstrap 4 errors after I submit my React form?

How to open pop up modal when form submit sucessfully and hide pop after 2 mintues and redirect to another page in react js?

How can I submit a form using JavaScript?

How can I submit this form with selenium in python?

How can I allow user to submit a form