Reputation: 187
I have the following link setup on my /home
page in react. Once the link is pressed I want to pass the variable stockRow
to the /quotes
page.
<Link to = "/quotes" params={{ testvalue: stockRow }} >
On my /quotes
page I was wondering how to grab that variable?
export default function Quotes() {
let variable = //Grab stockRow
return (
);
}
Upvotes: 1
Views: 593
Reputation: 13682
Updated the answer with more research:
Below are the different approaches to pass data using Link
1st approach:
<Link to={{pathname: `quotes`, search: `?testvalue=${stockRow}`}} />
const search = props.location.search;
const params = new URLSearchParams(search);
const testvalue = params.get('testvalue');
console.log(testvalue);
2nd approach:
<Route path="/quote/:testvalue" component={Quotes} />
<Link to=`/quotes/${stockRow}`>Go To Quotes Page</Link>
export default function Quotes(props) {
let variable = props.match.params.testvalue
return (
);
}
3rd approach:
to
prop of Link
as a state
value<Link to={{
pathname: '/quotes',
state: { testvalue: stockRow }
}}> My Link </Link>
export default function Quotes(props) {
let variable = props.location.state && props.location.state.testvalue
return (
);
}
Upvotes: 2
Reputation: 9769
You can access your passed value through router params
export default function Quotes({match}) {
const { testvalue } = match.params;
console.log(testvalue);
return (
);
}
Upvotes: 0