I have problem updating the nested state array, I have module where the user scan the QR code the value will be update the tracking number. Currently I used react js as my frontend and laravel as backend.
Problem: When I scan the QR code the tracking number is not set to the specific id.
Goal: I want to set the QR value to the specific id, not creating a new array.
Expected Output must be [id 33]:
"bookings": [
{
"id": 33,
"tracking_number": '83827172',
"status": "deliver",
"sub_status": "Completed",
"delivery_type": "regular",
"recipient": "tom",
"parcel": {
"id": 33,
"type": "bind",
"price": 95,
"description": "tshirt"
}
},
{
"id": 34,
"tracking_number": null,
"status": "pending",
"sub_status": "pending",
"delivery_type": "regular",
"recipient": "rey",
"parcel": {
"id": 33,
"type": "bind",
"price": 95,
"description": "pants"
}
},
],
Response JSON:
{
"id": 20,
"type": "pickup",
"address": {
"id": 65,
"country_id": 12,
"house_number": "002",
"created_at": "2022-07-30T07:11:41.000Z",
"updated_at": "2022-07-30T07:11:41.000Z"
},
"bookings": [
{
"id": 33,
"tracking_number": null,
"status": "deliver",
"sub_status": "Completed",
"delivery_type": "regular",
"recipient": "tom",
"parcel": {
"id": 33,
"type": "bind",
"price": 95,
"description": "tshirt"
}
},
{
"id": 34,
"tracking_number": null,
"status": "pending",
"sub_status": "pending",
"delivery_type": "regular",
"recipient": "rey",
"parcel": {
"id": 33,
"type": "bind",
"price": 95,
"description": "pants"
}
},
{
"tracking_number": "test"
}
],
}
Here is my handler function:
const ReadQRHandler = (e, index) => {
//note index is
const x = setScannedItems(prevState => (
{
...prevState,
bookings: [
...prevState.bookings,
{'tracking_number': 'test'}
]
}
));
console.log(x)
setIsStartScan(!isStartScan);
};
Very well appreciate your response...
Thank you.