how to pass props to the route when using react router <Link> how to pass props to the route when using react router <Link> reactjs reactjs

how to pass props to the route when using react router <Link>


You can pass it by mentioning the props in state

<Link  to={{    pathname: "/InvoiceDetails",    state: { name: 'jack', age: 25, city: 'Antwerp'}  }}/>

Read about it more here

And to use it in your next component you have to either wrap your component with a react-router-dom HOC withRouter or use useLocation hook they have provided.

EX- In your second component -

import {useLocation} from "react-router-dom";function Child() { let data = useLocation(); console.log(data); //state would be in data.state//}