I have created a functional component and state hook. I want to push all the checked items into a particular array of e.target.name of the useState hook.
const [formInput, setFormInput] = useState({
areaOfInterest: []
});
This handle I got help from an stackoverflow post: I want to store the checkbox datas into functional state hook but unable to do
const handleCheck = ({ target }) => {
const { name: blockName, checked, value } = target;
//console.log(value);
if (checked) {
formInputt[blockName].push(value);
} else {
const index = formInputt[blockName].indexOf(value);
formInputt[blockName].splice(index, 1);
}
setFormInputt(formInputt);
};
<Form.Group>
<Form.Label>Area of Interest</Form.Label>
<Form.Check
name="areaOfInterest"
label="Dedicated Teams"
onChange={handleCheck}
value="Dedicated Teams"
/>
<Form.Check
name="areaOfInterest"
label="Cloud Expert Advice & Support"
onChange={handleCheck}
value="Cloud Expert Advice & Support"
/>
<Form.Check
name="areaOfInterest"
label="Software Development"
onChange={handleCheck}
value="Software Development"
/>
<Form.Check
name="areaOfInterest"
label="Digital Transformation"
onChange={handleCheck}
value="Digital Transformation"
/>
</Form.Group>
Every time the user clicks on a checkbox, I want to add the value to an array and filter out what isn't there.
I am finding this very difficult and I think it should be quite simple. Can someone point me in the right direction? Is there a way to simply have a function trigger that reads the checked/unchecked status of my form input elements and then updates my state array accordingly?