reactjs - 如何通过锚标签传递道具?
问题描述
我想知道,如何将道具传递给其他组件?就像objectid={top.id}
通过锚标签一样。
{lists.map((top) => {
return (
<li key={id}>
<a href={'/object/'+ slugify(top.title) } objectid={top.id}>
<img alt={top.title} src={top.image-url}/>
</a>
</li>
)})
}
我想将 {top.id} 传递给组件,该组件将在从给定的 href 转到 url 时呈现,我可以像这样访问它props.objectid
解决方案
For the image, you cannot have src={top.image-url}
. You need to use src={top["image-url"]}
else it won't work.
And if you're using React Router, please use the React Router: Link
component for this.
<Link
to={{
pathname: `/object/${slugify(top.title)}`,
state: { objectId: top.id }
}}
>
<img alt={top.title} src={top["image-url"]} />
</Link>
Here you can also set the Link
state, so that it can be accessible in the other page too, so as from which page this has come from, etc.
推荐阅读
- google-analytics - GA BigQuery:使用自定义维度计算浏览量
- c++ - 什么是矢量
它甚至有效吗? - import - 将数据从 excel 导入到 teradata 错误错误代码 3706 Expected something between ')' 和 Insert
- r - 简单的 for 循环将列添加到列表中的数据框不起作用,这是怎么回事?
- java - java - 如何在java的命令行中屏蔽密码并显示光标移动?
- java - 静态内容页面在部署在 Kubernetes 集群中的 springboot 应用程序中不起作用
- excel - 如何获取任何 excel 文件并按标题名称将多列复制到新工作簿中(100,000 多个数据点)
- php - 自定义 WooCommerce“产品”列表 HTML 布局
- python - 获取列中值的计数并在图中显示它们的百分比
- php - 从服务器中删除 WordPress 文件