简体   繁体   English

带输入onChange的Array.map()始终获取列表中的最后一项

[英]Array.map() with input onChange always gets the last item on list

I have an array of objects in my state that I'm iterating through using map() . 我的状态中有一个对象数组,我正在使用map()迭代。 Each iteration has two tablerows inside a Fragment, with the second tablerow having an input tag that has an onChange() event that passes the current index to a function. 每次迭代在Fragment中都有两个tablerow,第二个tablerow有一个输入标记,该标记有一个onChange()事件,它将当前索引传递给一个函数。 I've noticed that it always passes the last index in the array. 我注意到它总是传递数组中的最后一个索引。

{this.state.billingStatementData.map((row, index) => {
  return (
    <Fragment key={index}>
        <TableRow hover>
            ...
        </TableRow>
        <TableRow>
            <TableCell>
            ...
              <input onChange={() => {
                    console.log(index);
                }}/>
              ...

Is it possible to get the current row or index of the iteration rather than the last item? 是否可以获取迭代的当前行或索引而不是最后一项?

Thank you. 谢谢。

Use props on input 输入上使用道具

<input index={index}...

And you can use this attribute in onChange. 您可以在onChange中使用此属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM