Skip to content
Advertisement

Updating the checkbox value based on the backend reponse using react

I’m trying to do an edit function, Where I’ll get the data from the backend. So if the value of dependents is ‘1,2’ then box the checkbox should be selected. I should be able to unselect the checkbox and send the value again if needed. In the below one, When we click on the add for the second row I’m checking if value of check box as 2 (line:79) and it’s showing as checked. The problem is when I’m unable to unselect it and send it again.

https://codesandbox.io/s/naughty-rhodes-i2sn9?file=/src/App.js

Advertisement

Answer

I have changed your code a bit, dependents state cannot handle dependents of all form values, so I have added a dependent object inside the form values object, I have tested it and it is working, Only the code and logic for checkbox is modified all other codes is similar to yours

https://codesandbox.io/s/react-fiddle-forked-ubcz4

try this

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement