I'm facing a problem, I have a React state which is an array.
When an item is added to the array with setState, I would like to delete it after for example five seconds.
Something like :
const {messages, setMessages} = useState([])
useEffect(() => {
const timer = setTimeout(() => {
setMessages(arr => arr.splice(i, 1))
}, 5000)
return () => clearTimeout(timer)
})
Where i is the index of the item to delete.
As a concrete example: I click every second in a button, this add an item to the array. After 5 seconds, the first item is deleted. After 6 seconds, the second item is deleted. etc...
Thanks for your help.