1
const submit = e => {
    e.preventDefault();
    if(homeEmail === "") {
        alert("Please Provide A Valid E-Mail")
    }
    else {
        db.collection("Home E-Mail").add({
            email: homeEmail
        }).then(() => 
            <Redirect to="/thank-you" />
        ).catch(() => 
            alert("Oops! Something Went Wrong, Please Try Again!")
        )

        setHomeEmail("")
    }
}

I've tried <Redirect /> to redirect to '/thank-you' page after .then but there is no redirection. How can I do this using react-router. If not possible using react-router, then how to do this using express

  • 2
    How about using `history.push('/thank-you')` – Germa Vinsmoke Feb 12 '21 at 08:37
  • I think this post will answer your question: [https://stackoverflow.com/questions/31079081/programmatically-navigate-using-react-router](https://stackoverflow.com/questions/31079081/programmatically-navigate-using-react-router). – Bram Dekker Feb 12 '21 at 08:39
  • Yes, some imperative navigation with `history.push` is in order, versus declarative with the `Redirect` component. – Drew Reese Feb 12 '21 at 08:59
  • @GermaVinsmoke Thanks! It resolved my issue!. How can we achieve the same using express? – Ayush Vishwakarma Feb 13 '21 at 09:03
  • @Ayush Maybe you can send the route in form of response data, like, `route: '/thank-you` and for the frontend URL you can save it in some `config` file. `${config.Domain}/res.route`. The `res.route` will obviously depend upon how you'll resolve data coming from express – Germa Vinsmoke Feb 13 '21 at 11:41
  • @GermaVinsmoke Thanks! I'll try this! – Ayush Vishwakarma Feb 14 '21 at 12:45

1 Answers1

1

Instead of using <Redirect />, you can use history.push('/thank-you') to redirect after your promise is resolved.

Germa Vinsmoke
  • 3,541
  • 4
  • 24
  • 34