javascript - 在 React-router Link 中将对象作为 prop 传递

标签 javascript reactjs typescript react-router react-router-v4

我正在获取 ProductList 中的产品列表,其中,我需要将选定的产品对象传递给 Product .

目前,我正在尝试通过 id作为路由参数并再次获取产品对象。但我想从 ProductList 发送整个产品对象至 Product .

我的路线是

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

ProductList 组件链接

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

如何将产品对象传递给 Product作为 Prop ?

下面的代码在 Typescript 中抛出错误,表示 Link 上不存在以下属性类型。

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

我尝试了以下问题,但似乎都没有我的问题。

最佳答案

Link 的“to”属性可以接受一个对象,所以你可以像这样传递你的 Prop :

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

然后你应该能够在 this.props.location.myCustomProps 中访问它们

关于javascript - 在 React-router Link 中将对象作为 prop 传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48949479/

相关文章:

javascript - 使五个宽度为 25% 的左浮动元素继续 float 到屏幕外

javascript - 过渡时背景无溢出

reactjs - 在 react 中使用 typescript ,无状态组件无法分配给类型 'React.SFC'

javascript - 带有带有 React 和 Material UI 的复选框的过滤表

angular - 如何在 PrimeNG 的 FileUpload 组件中中止文件上传?

javascript - 类型错误 : Unable to get property 'ngMetadataName' of undefined or null reference on IE 10

javascript - React - 重构类组件以将部件移动到功能组件

javascript - 我如何从 nth-child() 在 javascript 中分配颜色?

javascript - ReactJS - 渲染前等待第三方脚本响应

javascript - 为什么我的 React 组件在所有路由中渲染?