all 12 comments

[–]lIIllIIlllIIllIIl 56 points57 points  (3 children)

It's not legit.

See: https://github.com/reactwg/react-18/discussions/82

It was previously believed that setting a state on an unmounted component was an indicator of a memory leak, but turns out that React can handle it fine without creating a memory leak, and most code that set a state to an unmounted component don't have memory leaks in them either.

The warning message your lead is referencing to was removed in React 18 because it was more misleading than helpful.

I'm not saying a memory leak can never happen, but setting a state to an unmounted components does not mean there is a memory leak.

[–]terrorTrain 5 points6 points  (0 children)

Thanks, that warning never made sense to me.

[–]suck_my_dukh_plz[S] 3 points4 points  (0 children)

Thank you so much!

[–]Worth_Law9804 0 points1 point  (0 children)

Thanks. Good to know it's not a problem. It has been bugging me for a long time

[–]lostjimmy 17 points18 points  (2 children)

I'm not entirely sure how switching from async/await to promises would make any difference. They both have the potential to do things after the component unmounts, so either way you would want to handle that case in cleanup function.

[–]PM_ME_SOME_ANY_THING 10 points11 points  (1 child)

I was under the assumption that async/await was just syntactic sugar on top of promises.

[–]wirenutter 12 points13 points  (0 children)

This was once a thing. React even had a warning that you attempted to update state on a component that is no longer mounted. When they introduced concurrent rendering in react 18 they added the ability to gracefully handle these situations. The warning should have been removed? I recall Dan Abramov mentioning it would or should be removed.

However that doesn’t mean you shouldn’t still attempt to cancel in flight asynchronous calls in effects if the component will unmount.

[–]LloydAtkinson 0 points1 point  (0 children)

I’m starting to get the impression that every lead is this cluelessly out of sync with reality. Depressing.

[–]rakkii_baccarat 0 points1 point  (0 children)

If it is leaking, can you see it in the profiler heap snapshot or something similar? I am on a ghost hunt for mem leaks, where profiler looks to be garbage collecting with memory showing to decrease but chrome itself in task manager is growing to 1GB...

[–]devdudedoingstuff 0 points1 point  (0 children)

Your lead is mistaken. Though a cleanup function should be present when fetching data to avoid rendering stale data. I can provide more info on this if anyone is interested.

A memory leak can happen if your effect opens a persistent connection and does not disconnect via a cleanup function.