DEV Community

Discussion on: React: Class Component VS Function Component with Hooks

Collapse
 
joranbeasley profile image
joranbeasley

I think you left out the listener filter (empty for setup and tear down ... otherwise this gets called on every update to any state.

useEffect(() => { // setup (componentDidMount)
        const tick = setInterval(() => {
            changeTime()
        }, 1000)
        return () => { // teardown (componentWillUnmount)
              clearInterval(tick)
        }
    },[/* empty filter for setup/teardown */])
Enter fullscreen mode Exit fullscreen mode