This piece of code works fine and increments the count every second.
import React, { useEffect, useState } from 'react'
function IntervalHookCounter() {
const [count, setCount] = useState(0)
const tick = () => {
console.log("Called tick")
setCount(prevCount => prevCount+1)
}
useEffect(() => {
const interval = setInterval(tick, 1000)
return () => {
clearInterval(interval)
}
}, [])
return (
<div>
<h1>{count}</h1>
</div>
)
}
export default IntervalHookCounter
But this piece of code only goes up 1 count and then stops
import React, { useEffect, useState } from 'react'
function IntervalHookCounter() {
const [count, setCount] = useState(0)
const tick = () => {
console.log("Called tick")
setCount(count+1)
}
useEffect(() => {
const interval = setInterval(tick, 1000)
return () => {
clearInterval(interval)
}
}, [])
return (
<div>
<h1>{count}</h1>
</div>
)
}
export default IntervalHookCounter
Any idea why this might be the case. Please help this is getting really confusing.