I have project for booking. User selects days and make reservations. This reservation will be saved to mongo. Then I am fetching data from mongo and displaying it, user has 30 min for payment. If user doesn't make payment, reservation will be deleted from mongo and it will be available to another users.
here is useState to show timer
const [timeOut, setTimeOut] = React.useState(0)
here is one line to displaying data
<th>{item.status === 'unPaid' ? `${formatTime(item.createdAt)}` : ''} </th>
then formatTime function here
const formatTime = (time) => {
setInterval(() => {
// 1800000 = 30 min
const timer = new Date(time).getTime() + 1800000
let countDown = new Date().getTime()
let distance = timer - countDown
let min = Math.floor((distance % (1000*60*60) / (1000*60)))
let sec = Math.floor((distance % (1000*60) / (1000)))
//console.log(min, sec) <<<<< 30 min
let res = min + ':' + sec
setTimeOut(res) // <<< here is conflict
},1000)
return timeOut
}
I have problem, if user makes more than 1 reservation, I have conflict in setTimeOut(res) , It will store 2 differcent values in one spot.
Here is question, how can I fix it? How can I automaticly create new spot for timer and display it. More reservation === more useState, or any solution. If you have any idea how to fix it, please share it. Thanks
I hope it is clear!