Hacker News new | ask | show | jobs
by pcthrowaway 1400 days ago
I misunderstood what you were saying about it running on unmount when the dependency array is empty.

Of course, it does run on unmount when the dependency array is empty, and it runs on unmount when there are dependencies in the dependency array.

But upon re-reading what you said, I think your intent was that it's comparable to componentWillUnmount() in class-based React only when the dependency array is empty (because otherwise the cleanup function also gets called when dependencies change).

My apologies, as I never really used class-based React, so the distinction was lost on me (and also forgot about the cleanup function being called between useEffect callback calls)

1 comments

Oh gotcha, no worries. I'm not sure if you meant to reply to me anyway, my comment was in regards to knodi123's original comment that linked to an article of mine and said it was wrong about when useEffect re-renders – but the comment was edited and that part is gone, so now mine looks entirely crazy haha.
I edited it because I realized I was wrong - I was confused by the difference between no 2nd argument, and [] for the 2nd argument. But again - as a full time professional web developer, who has being using React for a few years, I think that also goes towards my point. ;-)
Hahah, yeah, this whole set of threads just hammers home your point, really :D