How can I write this better , I want to do it with setState instead of this.state.floors.push as I know that is bad practice but I couldnt figure it out. Im using react native.
FloorAPI.getFloorsByBuildingID(this.state.buildingID).then((response) => response.d.data.map((value) => {
console.log(value.floorName)
this.state.floors.push({value: value.floorName})
}))
// Create a new array based on current state:
let floors = [...this.state.floors];
// Add item to it
floors.push({ value: floorName });
// Set state
this.setState({ floors });
For now, the best possible and simplest way is
this.setState(previousState => ({
floors: [...previousState.floors, {"value": value.floorName}]
}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With