Skip to content

Doubt regarding the lesson : How to deal with side effects #27805

Closed Answered by MaoShizhong
coding-cosmos asked this question in Q&A
Discussion options

You must be logged in to vote

The lesson verbiage is correct. The example has the interval set in a useEffect with an empty dependency array, meaning the effect callback runs once on component mount, then never again. The cleanup function runs before every re-run of the effect, and once upon component unmount.

Since the empty dependency array means the effect will never run again after it runs on mount, then the only time the cleanup function runs will be when you unmount the component.

If the dependency array contained variables, then the effect would rerun every time once of those variables changes value, in which case the cleanup function will run before each of those effect reruns.

But otherwise, effects do not re…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@coding-cosmos
Comment options

@MaoShizhong
Comment options

Answer selected by coding-cosmos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants