【发布时间】:2019-03-04 20:39:30
【问题描述】:
在调用this.setState 之后,talents 属性仍然保持旧值:
onTalentModalCheckboxChange(e) {
debugger;
var talent = JSON.parse(e.target.dataset.talent);
talent.checked = !talent.checked;
if (this.maximumSelectedTalentsReached() && talent.checked) return;
const talentIndex = this.state.talents.findIndex(t => t.Id == talent.Id);
let updatedTalents = [...this.state.talents];
updatedTalents[talentIndex] = talent;
this.setState({ talents: updatedTalents });
// this method (setSearchResultsTotal) uses this.state.talents,
// but the talent that is updated here, is not updated.
// It still has the 'old' checked value
this.setSearchResultsTotal();
}
talents 属性包含 Talent 对象列表,这些对象都具有 checked 属性。我的问题是在设置更新的天赋对象时
我在这里错过了什么?
【问题讨论】:
标签: reactjs