gpt4 book ai didi

javascript - 使用 setState 更改状态中的对象数组

转载 作者:行者123 更新时间:2023-12-03 00:38:54 25 4
gpt4 key购买 nike

import React, {
Component
} from "react";
class App extends Component {
state = {
RSVP: [{
name: ["IronMan"],
isConfirmed: false
}]
};
handleChecked = () => {
this.setState({
isConfirmed: true
}););
};
render() {
return ( < div > {
this.state.RSVP && this.state.RSVP.length != 0 ? this.state.RSVP.map(({
name,
isConfirmed
}) => name && name.length != 0 ? ( <
div key = {
name
} > {
name
} < input type = "checkbox"
checked = {
isConfirmed
}
onChange = {
this.handleChecked
}
/></div > ) : null) : null
} < /div>);}}
export default App;

最佳答案

您可以更改数组内对象的状态,您可以使用以下源代码:

handleChange = (index) => {
let tmp = [...this.state.RSVP];
tmp[index] = {...tmp[index],isConfirmed: true};
this.setState({RSVP: tmp})
}

关于javascript - 使用 setState 更改状态中的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53542273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com