Changing a React State Object with Spread Operator
Solution 1:
The answer here is useReducer() to modify only portions of the state. https://reactjs.org/docs/hooks-reference.html#usereducer.
const errors = {
name: false,
email: false,
message: false,
};
const reducer = (state, action) => {
return {...state, ...action};
};
const [error, updateError] = useReducer(reducer,
errors
);
function validateSubmit(e) {
let response = true;
if (!values.name) {
updateError({name: true });
response = false;
}
if (!values.email) {
updateError({email: true });
response = false;
}
if (!values.message) {
updateError({message: true });
response = false;
}
return response;
}