Categories
JavaScript Answers

How to pass props to the Redirect component with React Router?

Spread the love

Sometimes, we want to pass props to the Redirect component with React Router.

In this article, we’ll look at how to pass props to the Redirect component with React Router.

How to pass props to the Redirect component with React Router?

To pass props to the Redirect component with React Router, we put it in the object we set as the value of the to prop.

For instance, we write

<Redirect
  to={{
    pathname: "/order",
    state: { id: "123" },
  }}
/>;

to set the to prop to an object with the state property set to an object with the props.

Then we can get the value of id in the destination route component with

this.props.location.state.id

Conclusion

To pass props to the Redirect component with React Router, we put it in the object we set as the value of the to prop.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *