javascript - React - onClick() 事件不在地图功能中显示点击项目的信息
问题描述
我有呈现列表中所有项目的数据。并且有一个导航按钮适用于单击并显示 2 个选项(编辑、删除)。问题是当我单击一个项目导航按钮时,它会显示所有项目的选项。它只需要触发一项的 onClick 功能
const [drawerIsOpen, setDrawerIsOpen] = useState(false);
const closeDrawer = () => {
setDrawerIsOpen(false)
}
const openDrawer = () => {
setDrawerIsOpen(true);
}
下面是渲染项目
bikes.map(bike => {
<div className="bike-item">
<small> {bike.title} </small>
</div>
<div onClick={()=> openDrawer(bike.id)} className="bike-item__actions_nav">
<i className="fas fa-ellipsis-v"></i>
</div>
<div className={`bike-item__children_actions ${drawerIsOpen && 'visible'}`}>
<Link to={`/update/${bike.id}`} className="bike-item_actions_icon">
Edit
</Link>
<Link className="bike-item_actions_icon">
Remove
</Link>
{drawerIsOpen && <Backdrop transparent onClick={closeDrawer} />}
</div>
我知道项目的索引有问题,但实际上无法解决。
这是示例:https ://codesandbox.io/s/laughing-fast-swf1o?file=/src/App.js
解决方案
openDrawer
调用函数时需要过滤自行车。
假设您的状态filteredBikes
最初将等于您所有的自行车。
然后,当您将所选自行车的 id 传递给 时,openDrawer
您需要根据 的当前状态更新此状态filteredBikes
,选择 id 等于传递的自行车。
推荐阅读
- vba - 根据不同日期的评级将缺失的评级分配给日期
- networking - 运行 6.1.3 的 Extreme VSP 交换机的 Ansible 剧本错误
- javascript - 使用 Ajax 时如何在自定义列表上显示查找字段
- html - 我不能在表格中使用垂直滚动,在该表格所在的容器中使用水平滚动
- html - 如何使导航响应?
- matlab - 直方图匹配结果与输入不相似
- amazon-web-services - 当我尝试使用 boto3 创建“put_bucket_replication”时出现“ACCESS_DENIED”错误
- r - 如何在 summarise_all、summarise_if 等中引用特定变量?
- flutter - 关闭接收无限数据流的主题的最佳方法是什么?
- python - 有没有办法从 SQS boto3 接收消息,其中某个属性的值是我们指定的值?