Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sdymj84/c57531d2f32b443bec9c00647ecc97c0 to your computer and use it in GitHub Desktop.
Save sdymj84/c57531d2f32b443bec9c00647ecc97c0 to your computer and use it in GitHub Desktop.
[React] Update values in nested object in array in state
state = {
vehicles: [{
year: "",
make: "",
model: "",
color: "",
licensePlate: "",
state: "",
}],
}
handleVehiclesChange = (e) => {
e.persist() // To fix Synthetic Events error
this.setState(prevState => {
const vehicles = prevState.vehicles.map(vehicle => {
return {
...vehicle,
[e.target.id]: e.target.value
}
})
return { vehicles }
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment