- build the application with
next build - start the application with
next start - click the numbers to see the console log
function Test1() {
const [expire, setExpire] = useState(5)
useEffect(() => {
const timer = setInterval(() => {
setExpire((prev) => prev - 1)
}, 1000)
if (expire === 0) {
clearInterval(timer)
}
return () => clearInterval(timer)
}, [expire])
const onClick = () => {
console.log('isExpired', isExpired)
}
const isExpired = expire === 0
return <div onClick={onClick} className="text-red-500">{expire}</div>
}The onClick function should be able to access the latest value of isExpired