How to use Redirect in the new react-router-dom of Reactjs

You have to use setState to set a property that will render the <Redirect> inside your render() method.


class MyComponent extends React.Component {
  state = {
    redirect: false

  handleSubmit () {**/)
      .then(() => this.setState({ redirect: true }));

  render () {
    const { redirect } = this.state;

     if (redirect) {
       return <Redirect to='/somewhere'/>;

     return <RenderYourForm/>;

You can also see an example in the official documentation:

That said, I would suggest you to put the API call inside a service or something. Then you could just use the history object to route programatically. This is how the integration with redux works.

But I guess you have your reasons to do it this way.

Leave a Comment