Javascript 是否未调用setState回调函数?
我是新来的。在这里,我有一个选择所有元素的复选框。单击该按钮时,我调用一种方法:Javascript 是否未调用setState回调函数?,javascript,reactjs,redux,Javascript,Reactjs,Redux,我是新来的。在这里,我有一个选择所有元素的复选框。单击该按钮时,我调用一种方法: handleCheckBox = (event) => { if (event.currentTarget.checked) { console.log("calling this"); const updates = {}; for (const item of [...this.props.untracked.content]) { updates[item.res
handleCheckBox = (event) => {
if (event.currentTarget.checked) {
console.log("calling this");
const updates = {};
for (const item of [...this.props.untracked.content]) {
updates[item.resumeId] = "unTracked";
}
for (const item of [...this.props.tracked.content]) {
updates[item.resumeId] = "tracked";
}
this.setState(oldState => ({
selectedValue: {
...oldState.selectedValue,
...updates,
}
}, () => {
console.log("going in this");
}));
} else {
const reset = {};
this.setState({
selectedValue: {
...reset
}
})
}
}
当我点击复选框时,它会选择所有的值以及它们各自的复选框。在这样做的过程中,进入控制台会被打印出来。现在,如果我单击相应的复选框1或2,然后如果我尝试单击调用给定方法的复选框,那么这次不会调用它
因此,这次没有调用callfunction。那么,有什么理由不打电话吗
this.setState(oldState => ({
selectedValue: {
...oldState.selectedValue,
...updates,
}
}, () => {
console.log("going in this");
}));
相当于
this.setState(oldState => () => {
console.log("going in this");
});
因为天气原因。您可能打算向调用传递两个参数,如下所示:
this.setState(oldState => ({
selectedValue: {
...oldState.selectedValue,
...updates,
}
}), () => {
console.log("going in this");
});
请注意括号放置方式的不同。理论上,它只会在每次奇数次单击时被调用,对吗?考虑到您实现的逻辑。