Open RanaMuzamal opened 11 months ago
@RanaMuzamal , in the above code, the checked
attribute is not controlled by formik due to which its not able to change the state of checkbox. Try setting the InitialValues
obj instead,
<Formik
initialValues={{
read: `your logic here`
write: `your logic here`
update: `your logic here`
delete: `your logic here`
}}
>
{({formik}) => (
<Form>
<label>
<Field type="checkbox" name="read" onChange={formik.handleChange} value={formik.values.read} >
read
</labe>
</Form>
)}
</Formik>
using above, you won't need to specify the checked
attribute as formik will handle the initialValue of checkbox from the initialValues
obj. You might also need to refactor your code.
Below is the source code for my form. I am using Formik for form handling. Essentially, users are granted permissions for specific modules, with available permissions being read, write, update, or delete. The form includes checkboxes corresponding to the permissions the user already possesses. However, users are currently unable to uncheck these already checked checkboxes. I appreciate any assistance in resolving this issue.
); };
export default AssignPermissionForm;`
Current Behaviour But not be be able to uncheck already checked
Expected Behaviour The user should be able to uncheck already checked checkbox or check any uncheckbox
Version "formik": "^2.4.2", "react": "^18.2.0",