When working with React’s useEffect hook we can add what’s called a “cleanup function”. This function is used to cancel any side effects that no longer require execution, before the component is unmounted. Why are cleanup functions useful? Using a cleanup function inside useEffect helps to prevent memory leaks or other errors when a side …
Category Archives: React
Async Functions in React useEffect
When using React’s useEffect hook we may want to write an asynchronous function, perhaps to fetch some data. There are some important considerations when combining async with useEffect, discussed further below. Don’t Do This The wrong way to write an async function with useEffect looks something like this: The main problem with the code above …