I'm building a React 16.13 application. I want to copy a property from my state in order to manipulate it and leave the underlying state unchanged. I thought this was the way to do it ...
async handleFormSubmit(e) {
e.preventDefault();
const NC = [...this.state.newCoop]
delete NC.addresses[0].country;
try {
const response = await fetch(FormContainer.REACT_APP_PROXY + "/coops/", {
method: "POST",
body: JSON.stringify(this.state.newCoop),
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
});
However this line
const NC = [...this.state.newCoop]
gives the error
Unhandled Rejection (TypeError): this.state.newCoop is not iterable
What's the right way to copy a state variable by value?