简体   繁体   English

无法在反应路由器 dom 中使用 Link 传递道具

[英]can't pass props using Link in react router dom

在此处输入图像描述


 <Link to={{pathname:"/Watch", movie:movie}} >

using this to pass props over Watch page but can't get any value.

I am new to react so there might be silly mistake or might be insufficient information.我是新手,所以可能会出现愚蠢的错误或信息不足。 please let me know regarding any extra info请让我知道任何额外的信息

If you are using react-router-dom v5 then route state is a property on the to object of the Link .如果您使用的是react-router-dom v5,则 route state 是Linkto object 上的属性。

RRDv5 Link RRDv5 链接

<Link to={{ pathname: "/Watch", state: { movie } }}>...</Link>

If using react-router-dom v6 then state is a top-level prop.如果使用react-router-dom v6,则state是顶级道具。

RRDv6 Link RRDv6 链接

<Link to="/Watch" state={{ movie }}>...</Link>

If you want to pass any state to the component which gets rendered on the path given, you need to paas it in state property of the "to" object as shown here - https://v5.reactrouter.com/web/api/Link/to-object If you want to pass any state to the component which gets rendered on the path given, you need to paas it in state property of the "to" object as shown here - https://v5.reactrouter.com/web/api/链接/对象

So just change your code as below -所以只需更改您的代码如下 -

 <Link to={{pathname:"/Watch", state :{movie:movie}}} >

Also, please check the version of react-router in your package.json.另外,请检查 package.json 中 react-router 的版本。 Based on that check the react-router documentation.基于此检查 react-router 文档。 Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM