【发布时间】:2020-04-18 11:52:01
【问题描述】:
我尝试将名为mendatory1 和mendatory2 的特定字符串推入状态中的空数组中。
class SignUp extends React.Component {
constructor() {
super();
this.states = {
box1: false,
box2: false,
box3: false,
box4: false,
agreeBox: []
};
}
}
当onClick 事件发生时,SingleClicked 函数激活并将输入检查值从 false 更改为 true,这意味着输入的复选框类型被勾选。
此时我想通过setState更新一个空数组的状态!
SingleClicked = e => {
console.dir(e.target);
if (e.target.className === "serviceTerm") {
this.setState({
box1: !this.state.box1,
agreeBox: !this.state.box1
? this.state.agreeBox.push("mendatory1")
: this.state.agreeBox
});
}
if (e.target.className === "InfoSecurity") {
this.SecondClicked(e);
}
if (e.target.className === "PromotionSms") {
this.ThirdClicked(e);
}
if (e.target.className === "PromotionMail") {
this.FourthClicked(e);
}
};
如果我点击另一个复选框,onClick 事件函数 'SingleClicked' 将调用 'SecondClicked' 函数,该函数会将检查的输入值从 false 更改为 true。
这里,我给出了相同的逻辑代码,但是当onClick事件发生时会报错:
【问题讨论】:
-
嗨,欢迎来到 SO。请将您的实际代码添加到问题中,而不是图像中。请阅读Why not upload images of code on SO when asking a question?
标签: arrays reactjs push setstate