首頁  >  問答  >  主體

在React js中,如何使用useState更新物件的鍵值

const Row  = [
        {
            name: "jad",
            link :"/jad",
            calories :"52",
            Fat :"4",
            Carbs :"5",
            Protein :"41"
        },
        {
            name: "john",
            link :"/john",
            calories :"82",
            Fat :"42",
            Carbs: "35",
            Protein :"1"
        }
] 

const [myList, setMyList] =  React.useState(Row);
function handle(){
  setMyList()
}
<TableBody>
        {
            myList.map((item)=> {
              return (
                <TableRow>
                    <TableCell><Link href={item.link}>{item.name}</Link></TableCell>
                    <TableCell align="right">{item.calories}</TableCell>
                    <TableCell align="right">{item.Fat}</TableCell>
                    <TableCell align="right">{item.Carbs}</TableCell>
                    <TableCell align="right">{item.Protein}</TableCell>
                     <TableCell align="right"><Button variant="contained">Edit</Button><Button variant="outlined" startIcon={<DeleteIcon />}>
  Delete</Button></TableCell>
                </TableRow>
              )
            })
        }
        </TableBody>

P粉066725148P粉066725148402 天前531

全部回覆(1)我來回復

  • P粉683665106

    P粉6836651062023-09-14 20:30:14

    透過循環解析myList,找到特定的鍵,更新鍵的新值,然後使用更新後的myList setMyList

    回覆
    0
  • 取消回覆