How should you pass props with the Redirect
component without having them exposed in the url?
Like this <Redirect to="/order?id=123 />"
? I'm using react-router-dom
.
How should you pass props with the Redirect
component without having them exposed in the url?
Like this <Redirect to="/order?id=123 />"
? I'm using react-router-dom
.
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.
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
<Redirect to={{
pathname: '/path',
state: { id: '123' }
}} />
Then you can access it via this.props.location.state.id
in the desired component
import { createBrowserHistory } from "history";
const withRefresh = createBrowserHistory({ forceRefresh: true });
const ROOT_PATH = process.env.PUBLIC_URL || "/myapp";
const useRedirectToAccounting = (params="1") => {
if(params){
withRefresh.push({
pathname: `${ROOT_PATH}/create`,
state: { id: `${params}` }
});
}
}
export default redirectToAccounting;
import useRedirectToAccounting from './redirectToAccounting
const handleOnClick = params => useRedirectToAccounting(params)
const RedirectorComponent = () => <a onClick={handleOnClick}>{"Label"}</a>
** this can be further refactored based on requirement.