简体   繁体   English

React - onClick() 事件不会在地图函数中显示被点击的项目信息

[英]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.

相关问题 在 React 中使用 onClick 事件 map function - Using onClick event in React map function 反应地图-如果丢失则不退还物品 - React map - don't return item if missing <span>内部<a>不触发Chrome中的“ onclick”事件</a></span> - <span>s inside <a> don't fire “onclick” event in Chrome map函数内部的ReactJS onClick事件,然后调用父函数 - ReactJS onClick event inside map function, then call parent's function 为什么 javascript function 不适用于 onclick 事件 - Why javascript function don't work for onclick event onclick事件:选择要通过ID单击的项目 - onclick event: select item to be clicked by id "如何在反应js中的array.map上的事件onclick上添加功能" - how to add function on event onclick on array.map in react js 我的onclick事件是在页面加载时执行的,而不是在单击按钮时执行的,我不知道为什么 - My onclick event performs when the page loads instead of when the button is clicked, and i don't know why 在反应中,我使用 map 显示了一堆项目。 在 onclick function 上,每个项目都会生效。我只想要一个被点击的人来更改 - In react I Have displayed Bunch of items using map. On onclick function every item gets effected .I want only one who was clicked to change 在地图功能中反应 onClick 功能 - React onClick function in map function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM