Here's a function to update the state:
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);
}
My console output is as shown below:
[PivotTable.js]:
PivotTable.js:43 ["advertiser"]
PivotTable.js:44 []
PivotTable.js:45 Temporary:
PivotTable.js:46 ["advertiser"]
My state looks like this:
state = {
header: {
ActiveHeaderIndex: this.props.ActiveIndex,
isHeaderAscending: true,
groupByHeader: []
}
}
Can anyone please help me in debugging where am I going wrong? The way I followed to setState
isn't that correct? temporary
is updated but setState
isn't updated for some reason.