I am working with a checkbox in react.
I am making my checkbox using dynamic data I am getting from a server and accordingly showing checked and unchecked states on the UI.
For form validation, I am using react-form-hook and for getting data onSubmit, I am using handleSubmit from react-form-hook
My only issue is that I am not able to get data as per my requirement.
My data:
let dta = [
{
p_id: 2,
p_name: "isco",
isChecked: true
},
{
p_id: 2,
p_name: "david",
isChecked: false
}
];
My form:
<form onSubmit={handleSubmit(submitForm)}>
{dta.map((li, index) => (
<div key={index}>
<input
type="checkbox"
id={li.component_name}
name={`data.${li.p_name}`}
ref={register}
defaultChecked={li.isChecked}
/>
<label htmlFor={li.p_name}>{li.p_name}</label>
</div>
))}
<button>Submit</button>
</form>
On Submit, I am getting data like this
{"data":[{"isco":false},{"david":true}]}
But how I want it is like below
{
"data": [
{
"p_name": "isco",
"isChecked": true,
"p_id":1
},
{
"p_name": "david",
"isChecked": false,
"p_id":2
}
]
}
Edit
handleSubmit.for seelet datainstead oflet dtacode sandboxwith full working code