I have this state in my Parent component
const [formData, setFormData] = useState({
firstName: "",
middleInitial: "",
lastName: "",
imageFile: "",
extension: "",
mobileNumber: "",
email: "",
agreement1: false,
agreement2: false,
agreement3: false,
});
and I pass it into the Child Component
as a props but having an issue changing the value of agreement1
, agreement2
and agreement3
which is a Boolean
setFormData({ ...formData, firstName: e.target.value })
This is the code in the Child Component
to update the state into the Parent Component
and it works perfectly fine. but when I try to update the Boolean I don't know how to access the previous boolean value before updating it.
I want to do something like this.
setBooleanValue((prevVal)=> !prevVal)
so every time I click on it . I toggle the value of the boolean
setFormData({...formData,agreement2: (prevVal) => !prevVal})
I try this but it doesn't work as expected
CodePudding user response:
you can try using the state update function as below:
setFormData((prev) => ({...prev,agreement2: !prev.agreement2}))