This is probably a super simple question but I seem to be finding various answers to. Essentially I have an app that makes an axios call to get a list of items via JSON API.
I need to set the initial state on the component load my state looks like this:
this.state = {
sensors: [],
filterName: "",
sortBy: "id",
};
my component loading code looks like so:
componentDidMount =() => {
axios
.get("http://localhost:3000/api/v1/devices.json")
.then((response) => {
// handle success
console.log(response);
this.setState({ sensors: [...response.data]});
})
.catch((error) => {
console.log(error);
})
}
This works...but I feel like this isn't the right way. Isn't it not supposed to change the initial array because I remember reading that we shouldn't ever be changing the initial state only returning a new version of it?
Is there a better/safer way?
(Also what about updating a single one/adding an item to array of objects? Is there a "preferred way")