No need to pass e. That is default behavior
onChange={handleChange}
Start by console logging the data.roles to see if the array updates with the checked ids
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have checkboxes for user roles. They are successfully displaying the correct roles for the user on my Edit.js form. But when I try to uncheck/check to edit the user roles, they can't respond.
Here's my <Checkbox /> component:
export default function Checkbox({ name, value, handleChange, checked, id, className }) {
return (
<input type="checkbox" name={name} id={id} checked={checked} value={value} className={`rounded border-indigo-300 text-indigo-600 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50` + className} onChange={(e) => handleChange(e)}
/>
);
}
This is the <Checkbox /> I have in my Edit.js form:
<Checkbox name="roles[]" id={`role${role.id}`} value={role.id} checked={data.roles.includes(role.id)} handleChange={handleChecked} required />
This is my handleChecked() event:
const handleChecked = (e) => {
let id = e.target.value;
e.target.checked
? setData("roles", [...data.roles, id])
: setData(
"roles",
data.roles.filter((item) => {
return item !== id;
})
);
};
Any help with this will be greatly appreciated. Thank you.
@Sinnbeck I found another way. I could just stick with the old handleChecked() method, but add the parseInt() to the id variable:
const handleChecked = (e) => {
//* use `parseInt()` to convert string to integer:
let id = parseInt(e.target.value);
e.target.checked
? setData("roles", [...data.roles, id])
: setData(
"roles",
data.roles.filter((item) => {
return item !== id;
})
);
};
And it works perfectly.
Please or to participate in this conversation.