Let’s change our component to log how many seconds have passed from the last time it is rendered. We will need to remove the dependency array to run the effect after every re-render.
If we keep re-rendering the component, we will keep creating new intervals. How do we only keep the last interval?
React also cleans up effects from the previous render before running the effects next time
Whenever you add an effect, think about what will happen when the component unmounts. Will this effect keep running? If so, provide a cleanup function. If you do that, it will automatically cover the second use case when effects accumulate with every execution.
This effect won't keep running after the component unmounts.
Doesn't require a cleanup.
useEffect(()=>{document.title= props.title})
This effect will keep running after the component unmounts.