Nespony
Nespony

Reputation: 1371

React redirect when promise resolved?

I have a project in react that uses react router. I want to redirect to another page when a promise is resolved like so:

  handleSubmit (event) {
    event.preventDefault();

    login(this.state.email, this.state.password).then(response => {

      return <Link to="/new_page"/>
    }).catch(err => {
        return "nothing";
    });
  }

In theory, when the response is successfully created, it should redirect to "new_page". However in practice nothing happens. Is there a way to do this?
Thanks!

Upvotes: 0

Views: 274

Answers (2)

Paul Fitzgerald
Paul Fitzgerald

Reputation: 12129

You can use history.push

handleSubmit (event) {
  event.preventDefault();
  login(this.state.email, this.state.password).then(response => {
    history.push('/new_page')
  }).catch(err => {
      return "nothing";
  });
}

Upvotes: 2

tpliakas
tpliakas

Reputation: 1129

You have to use

<Redirect to="/new_page"/>

or something like

history.push("/new_page")

Upvotes: 1

Related Questions