這是更新狀態(tài)的函數(shù): GroupByHandler = (index) => { const temporary = [...this.state.header.groupByHeader]; if(this.state.header.groupByHeader.includes(this.props.Headers[index])) { temporary.splice(index,1); // console.log("[PivotTable.js]: if statement under GroupByHandler"); // console.log(this.props.Headers[index]); } else { temporary.push(this.props.Headers[index]); // console.log("[PivotTable.js]: else statement under GroupByHandler"); // console.log(this.props.Headers[index]); // console.log(temporary); } this.setState({ header: { ...this.state.header, groupByHeader: temporary } }); console.log("[PivotTable.js]: "); console.log(temporary); console.log(this.state.header.groupByHeader); console.log("Temporary: "); console.log(temporary); }我的控制臺輸出如下所示:[數(shù)據(jù)透視表.js]:PivotTable.js:43 [“廣告商”]數(shù)據(jù)透視表.js:44 []PivotTable.js:45 臨時:PivotTable.js:46 [“廣告商”]我的狀態(tài)是這樣的: state = { header: { ActiveHeaderIndex: this.props.ActiveIndex, isHeaderAscending: true, groupByHeader: [] }}誰能幫我調(diào)試我哪里出錯了?我遵循的方式setState不正確嗎?temporary已更新但setState由于某種原因未更新。
React 將我的狀態(tài)更新延遲 1 次迭代
繁花不似錦
2023-04-27 14:58:59