I'm trying to update the state of particular index of my array when a function is called. However, the value is not updated despite clicking multiple times.
I have the following array extracted from the const EDITABLES
in componentDidMount
:
values = EDITABLES.map((data) => {
return {
id: data.id,
value: financials[data.id],
table: data.table,
add: false,
minus: false,
};
});
this.setState({
values,
});
When rendering I map the array like so:
{EDITABLES.map((data, index) => {
console.log(values[index]?.add, "bool"); //despite calling a function to change the state of add in values it remains the same
return (
<Row
key={index}
style={{
gridTemplateColumns: "22% 25% 2% 25% 25% 21%",
display: "grid",
}}
>
<p>
{data.name}
</p>
<div>
<CompleteButton //CompleteButton is a styled div
isAdd={values[index]?.add}
onClick={() => {
this.changeFalse(index) //function call
}}
>
</CompleteButton>
</div>
And this is what the changeFalse function looks like:
changeFalse(index) {
this.setState((prevState) => ({
values: {
...prevState.values,
[prevState.values[index].add]: !prevState.values[index].add,
},
}));
}
No matter how many times I click the button, it does not update and sti;; remains "false" and I'm not sure why