So I have a field in state bullet_points: []
that is an array of elements.
I have a function in my component that adds/removes elements from this array. To save the changes, clicking submit will sent product: this.state
as an argument in a POST request to my backend.
The backend only receives bullet_points
if the array IS NOT empty. If I clear the field of all elements, it is never sent to the backend.
What would cause this? Below are the relevant code snippets.
1) Function for add/remove of elements (notice arrow, this is where elements are removed)
updateList(evt) {
evt.preventDefault();
let elements = this.state[evt.target.dataset.type];
let idx = parseInt(evt.target.dataset.index);
let new_element = evt.target.dataset.type == "durations" ? 30 : "";
if (isNaN(idx)) {
elements.push(new_element);
} else {
if (evt.target.dataset.delete) { <----------
elements.splice(idx, 1);
}
else {
elements[idx] = evt.target.value;
}
}
this.setState({ [evt.target.dataset.type]: elements });
}
2) POST request to backend
API.put(`coach/products/${this.props.params.id}`, {
product: this.state
}, function(res) {
this.closeModal();
Alert.success('<p class="small-body-text">Your changes have been saved</p>', {
position: "top-right",
effect: "scale",
timeout: 5000
});
}.bind(this));