I have the following object in my React app:
const processDetails = () => {
const INIT_ERROR = {
condition: {
name: "",
description: ""
},
treatment: {
name: "",
description: ""
},
}
const [error, setError] = useState(INIT_ERROR);
}
This is connected to a form which takes various values; on submission, it needs to be validated and the value of 'error' is updated where information is missing.
In the validation function, I have, eg:
if(conditionName === "")) {
setError(...);
}
and I want this to set the value of error.condition.name to "Error - please enter a name for this condition".
How do I do this? I've tried:
setError({ ...error, condition: {...error.condition, name: "Please enter a name for this condition"} });
which seems to work, but then doing the same thing to update error.condition.description, like this:
setError({ ...error, condition: {...error.condition, description: "Please enter a description for this condition"} });
overwrites the value of error.condition.name.
...isn't an operator. Operators can't do what...does. It's just syntax.)