I have a state hook defined as:
const [formState, setFormState] = useState({
officeHourStart: "",
officeHourEnd: "",
timezone: "",
doctorQuotas: {}
});
and it is initiated like:
const mapUserIdQuotas = {0: 12, 1: 15};
setFormState({
officeHourStart: settings.officeHourStart,
officeHourEnd: settings.officeHourEnd,
timezone: settings.timezone,
doctorQuotas: mapUserIdQuotas
})
Now I want to update the doctorQuotas
part of it.
Specifically, I need to update the {0: 12, 1: 15}
to {0: 12, 1: 20}
const updateDoctorQuota = (event) => {
const id = parseInt(event.target.id);
const value = parseInt(event.target.value);
setFormState({...formState, doctorQuotas: { ...formState.doctorQuotas, id: value}});
}
This does not work since the result formState
would be:
{
officeHourStart: ... ,
officeHourEnd: ... ,
timezone: ... ,
doctorQuotas: {0: 12, 1: 15, id: 20}
}
I tried:
const updateDoctorQuota = (event) => {
const id = parseInt(event.target.id);
const value = parseInt(event.target.value);
setFormState({...formState, doctorQuotas: { ...formState.doctorQuotas, [id]: value}});
}
Then it becomes:
{
officeHourStart: ... ,
officeHourEnd: ... ,
timezone: ... ,
doctorQuotas: {0: 12, 1: 15, NaN: 20}
}
How can I use the integer value of variable id
to update the corresponding key-value pair?