I have defined an array as below
constructor() {
super();
this.state = {
resultingName : []
};
}
After this, I perform all the operation, get the response and then push to a temporary array
let tmpArray = [];
for (var i = 0; i < response.data.names.length; i++) {
tmpArray.push(response.data.names[i]);
}
Now, the data is present in the tmpArray.
What is the way that I can assign this to my initial resultingName array?
These are the ways that don't work
this.setState({
resultingName: this.state.resultingName.concat(tmpArray)
});
Also,
this.setState({ resultingName: [...this.state.resultingName, ...tmpArray] })
doesn't work.
What is the best way to assign to the array, since I need the data in other functions of the component.
Also, I want to display the contents of the resultingName array on the console. How do I do that?
setStateno need of extra calculationsthis.setState({ resultingName: [...this.state.resultingName, ...response.data.names] })resultingNameCannot read property 'setState' of undefinedAlso, the array will be nullthisis not pointing to class. check this and it will run as aspected