Can this.setState update the value in an array object?

    constructor(props) {
        super(props);
        this.state = {
          moreData: [
            {name: "a", value: "1"},
            {name: "b", value: "2"},
            {name: "c", value: "3"}
          ]
        }
    }

how to change the value whose name is a to "10" through this.setState

Mar.05,2021

setState can only change the value of the outermost property.

in your case, you need to manually modify it and then re-assign it.

for example:

changeValue() {
    const data = this.state.moreData.map(v => {
        if (v.name === 'a') {
            v.value = '10'
        }
        return v
    });
    this.setState({ moreData: data })
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3b621-2c297.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3b621-2c297.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?