[英]How to get the childNode index onClick in React in functional component
Coinsider the code below考虑下面的代码
function getChildIndex(){
//logic here
}
//render
<div>
{Array.from({length: 3}).map((item,index) => (
<div onClick={getChildIndex}/>
)
)}
</div>
it will render as follow它将呈现如下
<div>
<div> //with click event // 1st Child
<div> //with click event // 2nd Child
<div> //with click event // 3rd Child
</div>
How can i get the child element number when i click the child element单击子元素时如何获取子元素编号
The index
is available as 2nd argument of map's
callback function as you also showed.正如您还展示的那样,该
index
可用作map's
第二个参数。 Just use it as required like so:-只需像这样根据需要使用它:-
<div onClick={()=>doSomethingWithIndex(index)}/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.