I have task where I need to push the new data to the old state, the data is from response of socket.on, The problem right now when socket listener trigger memory leak or infinite loop results.
Expected Output (data pushed):
{
"id": 1,
"type": "pickup",
"vehicle": "Trucks",
"created_at": "2023-12-13T14:10:29.000Z",
"updated_at": "2023-12-13T14:10:29.000Z",
"bookings": []
},
{
"id": 2,
"type": "delivery",
"vehicle": "Motor",
"created_at": "2023-12-13T14:10:29.000Z",
"updated_at": "2023-12-13T14:10:29.000Z"
"bookings": []
}
Current Testing:
console.log(JSON.stringify(bookingList.length, null, 2))
State:
const [bookingList, setBookingList] = useState([])
Socket On:
socket.on("new_booking", (items) => {
setBookingList(currentBooking => [...currentBooking, items?.booking])
})
Here is the first data render:
{
"id": 1,
"type": "pickup",
"vehicle": "Trucks",
"created_at": "2023-12-13T14:10:29.000Z",
"updated_at": "2023-12-13T14:10:29.000Z",
"bookings": []
}
Here is the data from socket io:
{
"id": 2,
"type": "pickup",
"vehicle": "Motor",
"created_at": "2023-12-13T14:10:29.000Z",
"updated_at": "2023-12-13T14:10:29.000Z"
"bookings": []
}