๐Ÿฆ Cancel Properly HTTP Requests in React Hooks and avoid Memory Leaks ๐Ÿšจ

viclafouch

Victor de la Fouchardiรจre

Posted on July 29, 2020

๐Ÿฆ Cancel Properly HTTP Requests in React Hooks and avoid Memory Leaks ๐Ÿšจ

Hello guys ! ๐Ÿ‘‹

Today, let's take a look at cancelling a web request with fetch and Abort Controller in React Hooks! ๐Ÿค—

When we work with Fetch to manage data, we sometimes want to cancel the request (e.g. when we leave the current page, when we leave a modal, ...).

In the example below ๐Ÿ‘‡, we fetch the data to display when switching route. But, we leave the route/page before the fetch is completed.

React hooks memory leaks 1

๐Ÿšธ Demo 1/2

React hooks memory leaks 2

๐Ÿšธ Source code 2/2

We just saw a memory leak in action! ๐Ÿ’ช๐Ÿผ Letโ€™s see why this error occurred, and what it exactly means.

โ“ WHY A MEMORY LEAK?: We have a component that performs an asynchronous fetch(url) task, then updates the state of the component to display the elements, BUT we unmount the component before the request is even completed. The state of the unmounted component is updated (e.g. setUsers, setState), which follows a memory leak.

๐Ÿš€ Let's use the new AbortController API !

Abort Controller allows you to subscribe to one or more Web Requests with the ability to cancel them. ๐Ÿ”ฅ

Basics of AbortController

First of all, let's create a new AbortController object instance.

Abort Controller API

Now, we can access to controller.signal.

"The signal read-only property of the AbortController interface returns an AbortSignal object instance, which can be used to communicate with/abort a DOM request as desired." MDN Source

Let's see how to use it ๐Ÿ’ช

Abort fetch example 1

And finally, if we want to cancel the current request, just call abort(). Also, you can get controller.signal.aborted which is a Boolean that indicates whether the request(s) the signal is communicating with is/are aborted (true) or not (false).

Abort fetch example 2

โ—๏ธ Note: When abort() is called, the fetch() promise rejects with a DOMException named AbortError.

Yeah, you just learned how to cancel a Web Request natively! ๐Ÿ‘

๐Ÿคฉ Let's do this with React Hooks !

โŒ BEFORE

Below is an example of a component that retrieves data in order to display them:

Memory leak react hooks 1

If I leave the page too fast and the request is not finished: MEMORY LEAK

Memory leak react hooks 2

โœ… AFTER

So let's useEffect to subscribe to our fetch request and to avoid memory leaks. We use the clean method of useEffect for calling abort() when our component unmount.

No memory leaks react hooks

Now, no more memory leaks! ๐Ÿ˜

Example abort controller memory leaks

As always, feel free to reach out to me! ๐Ÿ˜‰

You can check this demo on abort-with-react-hooks.vercel.app.
Also, here the source code of this article in this gist.

Cheers ๐Ÿป ๐Ÿป ๐Ÿป

If you enjoyed this article you can follow me on Twitter or here on dev.to where I regularly post bite size tips relating to HTML, CSS and JavaScript.

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
viclafouch
Victor de la Fouchardiรจre

Posted on July 29, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About