Skip to content
Advertisement

How to add and remove multiple checkbox values and update nested array in React state hook

I am using React Context to create a multistep form, the form must keep the selection when the user clicks the next or previous step. I am using the below code and it’s working fine until I reach the stage to add multiple features using the checkbox, once items are checked, user can go to the previous step to edit and press next to go to the next stage where checked checkboxes must remain checked. I cannot figure out how to push each checkbox value to the features array and remove the item from array when the user uncheck. The important part is to retain the selected despite user go to previous or next step.

Context Provider

JavaScript

Car Details Component

JavaScript

Advertisement

Answer

You seem to be calling a non existent function handleFeaturesChange in you feature-item checkbox.

Anyway, something like this should work:

JavaScript

You could potentially replace the value with name string but then you’d need to update the condition in the checked param of the Checkbox to compare it with the name instead.

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