【发布时间】:2022-01-19 05:52:38
【问题描述】:
我从 API 接收数据,并按行显示数据,每一行都会有一个更新按钮,以便函数知道要更新哪个 id。
由于我有很多行,因此无法为每一行设置 setState 或 useRef,是否有任何解决方案可以获取该特定 ID 的用户输入?
示例:
const data = [{id:1, number:0},{id:2, number:0},{id:3, number:0} ...] // a lot of data
const update=(id)=>{
// get the new number of the id and call api to update only for the certain id
}
{data && data.map(x => {
return (
<div className={`row ${x.id}`} key={x.id}>
<div className='cell'>
{x.id}
</div>
<div className={`cell number`}>
{x.number}
<input className="cellInput" value={x.number} placeholder={x.number} />
</div>
<div className='cell'>
<button onClick={(() => update(x.id))}>Update</button>
</div>
</div>
)
})}
【问题讨论】: