When you use the
The below example sets a timer for 10 seconds. The alert message stays red until the 10 seconds timer is up and then turns green.
The initial time is set in the state variable
const [timer, setTimer] = useState(10)
Then we decrement this number by one every second. This function does the decrements. Since we use this method inside the
const decrementTimer = useCallback(() => {
setTimer((oldTimer) => oldTimer-1)
},[])
The
const timeoutFunction = setInterval(decrementTimer, 1000)
return () => clearInterval(timeoutFunction);
The
We want to stop the timer at 0. Otherwise, the countdown happens as long as the component stays mounted until it unmounts.
if(timer <= 0){
return
}
The above code in the
import { useCallback, useEffect, useState } from "react"
import { Alert } from "react-bootstrap"
const SuccessMessage = () =>{
const [timer, setTimer] = useState(10)
const decrementTimer = useCallback(() => {
setTimer((oldTimer) => oldTimer-1)
},[])
useEffect(() => {
if(timer <= 0){
return
}
const timeoutFunction = setInterval(decrementTimer, 1000)
return () => clearInterval(timeoutFunction);
},[decrementTimer, timer])
return(
<div>
<Alert
variant={timer > 0 ? "danger" : "success"}>
This message will turn green in {timer} seconds
</Alert>
</div>
)
}
export default SuccessMessage
The