這是我的代碼:const [data, setData] = useState([ {id: 1, name: 'paper', qty: 10}, {id: 2, name: 'bottle', qty: 10}, ]); const [isEditable, setEditable] = useState([]); useEffect(()=>{ data.map(each=>{ isEditable[each.id] = false; }) }) const handleEdit = (id) => { var arr = data; arr[id] = !arr[id]; setEditable(arr); } return ( <div> {data.map((row) => ( <TableRow key={row.id}> <TableCell>{row.id}</TableCell> <TableCell>{row.name}</TableCell> { isEditable[row.id] ? <TableCell> <TextField id="outlined-basic" size="small" variant="outlined" /> </TableCell> : <TableCell>{row.qty}</TableCell> } <TableCell> <button onClick={()=> handleEdit(row.id)}> Edit </button> </TableCell> </TableRow> ))} </div> )}我想單擊表格行上的編輯按鈕,文本字段僅出現在特定行上,但是當我單擊它時,文本字段出現在其他行上。這里有什么問題?
Reactjs:如何編輯特定行的表格?
長風秋雁
2022-12-22 14:32:49