Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an object as prop in React-router Link

I'm getting the list of products in ProductList, in which, I need to pass the selected product object to Product.

Currently, I'm trying pass the id as a route param and get the product object again. But I want to send the entire product object from ProductList to Product.

My Route is

<Route path={joinPath(["/product", ":id?"])} component={Product} />

ProductList component Link

<Link to={"/product/" + this.props.product.Id} >{this.props.product.Name} </Link>

How to pass product object to Product as a prop?

the below one throws error in Typescript saying the following property does not exist on Link Type.

<Link to={"/product/" + this.props.product.Id} params={product}>{Name}</Link>

I tried the following questions, but none seems to have my issues.

  • Pass props in Link react-router <--- this is similar to my issue, but answer doesn't work for react-router v4
  • react-router - pass props to handler component
  • React: passing in properties
like image 373
Prajwal Avatar asked Feb 23 '18 13:02

Prajwal


People also ask

How do you pass props in React router link?

To recap, if you need to pass data from Link through to the new component that's being rendered, pass Link s a state prop with the data you want to pass through. Then, to access the Link s state property from the component that's being rendered, use the useLocation Hook to get access to location. state .

Can you pass props through React router?

However, with React Router v6, since you're in charge of creating the element, you just pass a prop to the component as you normally would. Want to learn more? If you liked this post and want to learn more, check out our free Comprehensive Guide to React Router.

Can I pass an object as a prop React?

Use the spread syntax (...) to pass an object as props to a React component, e.g. <Person {... obj} /> . The spread syntax will unpack all of the properties of the object and pass them as props to the specified component.

Can you pass props through next link?

You can't pass data to next/link component. Even if you would pass it, you won't be able to access it on server-side when a user visits the page directly or refreshes it.


2 Answers

The "to" property of Link can accept an object so you can pass your props like this :

<Link to={
    { 
        pathname: "/product/" + this.props.product.Id,
        myCustomProps: product
    }
}>
    {Name}
</Link>

Then you should be able to access them in this.props.location.myCustomProps

like image 122
Dyo Avatar answered Oct 20 '22 17:10

Dyo


I would suggest using redux for retrieving the data. When you navigate to product route you can get the product details by dispatching some action.

componentDidMount() {
    let productId = this.props.match.params.Id;
    this.props.actions.getProduct(productId);
}

The product route should be connected with the the store. Hope this helps.

like image 29
Dev Avatar answered Oct 20 '22 18:10

Dev