I have multiple checkboxes which are created within map(loop). I am unable to add two functionalities:
-
When user click on any checkbox, it shoud show that checkbox checked
-
All event_id which are present in eventGoing variable should be checked by default.
{
products.map(post=>
// eventGoing going contains list of event ids which i extracted from DB
if(eventGoing.indexOf(event_id) > -1)
{
// need to check the checkbox if event_id is within eventGoing variable
}<input checked={checked} name="goingChk" value={"checkbox-"+post.event_id} type="checkbox" id={"checkbox-"+post.event_id} onClick={e => handleGoing(e, post.event_id)} /> <br>
);
}
Ignore the coding errors as i extracted the code from 2 different functional components.
Please help me out how can i get two functionalities mentioned above.
2
Answers
Complete code:
Problem:
the problem is you are saving value on single boolean state, which is causing issue (checking all checkboxes)
Solution
Here is something you can try
You can use replace products array with your products array in this code and that should work just fine.
Although if you want to do it inside your code you can do it like this
Sure here’s updated example
Complete code: