ReactJS - Pass props with Redirect component ReactJS - Pass props with Redirect component javascript javascript

ReactJS - Pass props with Redirect component


You can pass data with Redirect like this:

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

and this is how you can access it:

this.props.location.state.id

The API docs explain how to pass state and other variables in Redirect / History prop.

Source: https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/Redirect.md#to-object


You should first pass the props in Route where you have define in your App.js

<Route path="/test/new" render={(props) => <NewTestComp {...props}/>}/>

then in your first Component

<Redirect            to={{            pathname: "/test/new",            state: { property_id: property_id }          }}        />

and then in your Redirected NewTestComp you can use it where ever you want like this

componentDidMount(props){console.log("property_id",this.props.location.state.property_id);}


You can use browser history state like this:

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

Then you can access it via this.props.location.state.id

Source: https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/Redirect.md#to-object