Heim  >  Fragen und Antworten  >  Hauptteil

In React js erfahren Sie, wie Sie mit useState den Schlüsselwert eines Objekts aktualisieren

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粉066725148426 Tage vor555

Antworte allen(1)Ich werde antworten

  • P粉683665106

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

    通过循环解析myList,找到特定的键,更新键的新值,然后使用更新后的myList setMyList

    Antwort
    0
  • StornierenAntwort