Avoiding Memory Leaks

Learn how to mitigating memory leaks in React by using refs to handle asynchronous operations and prevent unwanted updates after component unmount.

Addressing memory leaks in async operations

Historically speaking, a ref was created to hold a DOM element, but people later found it very effective in addressing tricky problems. One problem is memory leaks, which happen when performing an async action. The thing about an async operation is that the callback function gets invoked later. By the time the callback is handled, there's a chance the component (or any variable associated with the component) is not valid anymore.

Let's say we fetch an API and display the result as text:

Get hands-on with 1200+ tech skills courses.