How to pass context and props to <Redirect> component in Reactjs

143 Views Asked by At

This is my requirement.

When a route /xyz (displays xyz Component) is hit on browser, I need to navigate to separate route /abc (displays abc component).

I also need to pass new props and context to the target component on navigation.

I am looking for something like

**

<Redirect from="/xyz" to={{pathName: "/abc", component={<AbcComponent context={context} prop1={prop1} prop2={this.props.location.search}}}} />

**

How to achieve this in Reactjs ?

1

There are 1 best solutions below

0
On

You can't use "component" key in "to" props of redirect

You can do this way to={{pathname="/abc",state={prop1=prop1,prop2=prop2} }}

<Redirect to={{pathName: "/abc", state={context:context,prop:prop1,prop2:props2} }} />

and

you can get data on /abc via props.location.state