[英]React - onClick() event don't show clicked item's information in map function
I have data that render all items in the list.我有呈现列表中所有项目的数据。 And there is a nav button works onClick and shows 2 option (edit, remove).并且有一个导航按钮可用于 onClick 并显示 2 个选项(编辑、删除)。 And the problem is when I click an item nav button it shows all of the items' options.问题是当我单击一个项目导航按钮时,它会显示所有项目的选项。 It has to trigger only one item's onClick function它只需要触发一项的 onClick 函数
const [drawerIsOpen, setDrawerIsOpen] = useState(false);
const closeDrawer = () => {
setDrawerIsOpen(false)
}
const openDrawer = () => {
setDrawerIsOpen(true);
}
Below is rendered items下面是渲染的项目
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>
I know there is a problem with the item's index but practically couldn't solve it.我知道项目的索引有问题,但实际上无法解决。
Here is example: https://codesandbox.io/s/laughing-fast-swf1o?file=/src/App.js这是示例: https : //codesandbox.io/s/laughing-fast-swf1o?file=/src/App.js
You need to filter the bikes when you call the openDrawer
function.调用openDrawer
函数时需要过滤自行车。
Say you have state filteredBikes
which initially will be equal to all of your bikes.假设您有状态filteredBikes
自行车,最初将等于您所有的自行车。
Then when you pass the id of the chosen bike into the openDrawer
you need to update this state based on the current state of filteredBikes
, choosing the bike whose id equals to the passed.然后,当你通过选择自行车的标识加入openDrawer
您需要根据当前状态来更新这个状态filteredBikes
,选择其ID等于传递的自行车。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.