Have y'all gotten that React warning about state updates after component unmount?

I get it mainly when running unit tests

Happens when:
- Make async request
- Component unmounts
- Response comes back
- Try to update state w/ new data

I've come up w/ 4️⃣ solutions

/thread 🧵👇🏾
Since vanilla JS Promises aren't cancellable the next best thing is to keep track of the mounted state & don't update state if the comp is unmounted

1️⃣ Variable to track mounted state

Quick & dirty, but only works in single useEffect w/ no deps

🧵👇🏾
2️⃣ Ref to track mounted state

All effects can check the ref regardless of if they have deps. Will have to duplicate in multiple components tho

🧵👇🏾
3️⃣ Custom Hook to track mounted state

For when multiple components need to know mounted state

(use of `useCallback` is key for preventing effects from being called every render)

🧵👇🏾
4️⃣ Custom Hook to fetch only when mounted

Moves check into the hook so async actions only resolve when component is mounted

React Query (s/o @tannerlinsley), use-http, and other packages have this baked into their Hooks

🧵👇🏾
This warning can also happen if we forget to unsubscribe an event handler. This is a memory leak!

But this is exactly what the clean-up function returned by `useEffect` is for. Always remember to remove handlers on unmount 😉

🧵👇🏾
That was a quick overview 😅

More details on these solutions for avoiding the warning when a component updates state after unmount in my latest post

benmvp.com/blog/handling-…
And there's more where that came from!

Subscribe to the BenMVP Newsletter and get weekly updates on all things React, TypeScript, "DivOps" and more! 🤓

benmvp.com/subscribe/?utm…

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Ben Ilegbodu 🏀👨🏾‍💻

Ben Ilegbodu 🏀👨🏾‍💻 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @benmvp

14 Jan
If you're getting started with React in TypeScript one of the first things you'll wanna do is migrate from runtime prop-types over to static TS types

Typing a function component is the same as any function in TS

Typically props are typed with a TS `interface`

/thread 🧵👇🏾
The primitive prop types map over to their TS equivalents...

🧵👇🏾
Special types like React nodes, enums, class instances, and others...

🧵👇🏾
Read 7 tweets
5 Jan
Using objects/arrays in deps for React useEffect can cause it to run the effect on *every* re-render even if contents are the same cuz the references are different

We've got a couple of options...

1️⃣ Depend on the obj properties instead of the entire obj

/thread 🧵👇🏾 ImageImage
2️⃣ Duplicate the object within the effect when the *entire* object is needed. useEffect then depends on the pieces needed to create the object

In general creating object literals is cheap so the perf wins from optimized useEffect should outweigh the duplication

🧵👇🏾 Image
3️⃣ Memoize/cache the object w/ useMemo Hook if creating the object is too expensive to do it twice. The obj is now safe to use in deps

This is suggested by the `react-hooks/exhaustive-deps` ESLint rule (part of `eslint-plugin-react-hooks` which you should totally be using)

🧵👇🏾 Image
Read 6 tweets
15 Dec 20
I used to tell folks learning React to learn JS really well first. I had good intentions but was still gate-keeping 😔

But I do think knowing modern JS can really level-up your React skills and I've got top 10 features I think will help you be more awesome-r 🔥🔥

[🧵 thread]
1. ES Modules

I see lots of confusion about when to use named vs default imports. The import style depends on how it was exported (named or default)

Named imports go within the curly braces & the single default import is outside of them 🤓 ImageImage
2. Arrow functions

Arrow functions can exist everywhere within your React app. Some folks (like myself) replace all functions with arrows while others only use them in spots

They really win w/ character savings but for those unfamiliar w/ the syntax they can lose w/ clarity 🧐 ImageImage
Read 12 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!