Stop useEffect React Hook re-render multiple times with Async call - Tutorial - useEffect cleanup.

  Рет қаралды 41,154

Dylan Albertazzi

Dylan Albertazzi

3 жыл бұрын

❗️Today I share a quick trick on how to stop unwanted responses from re-rendering a react component whose useEffect has an async function.
TLDR; Use useEffect cleanup. See the code here 👉 gist.github.com/dylan-alberta...
I ran across this problem when making an API call when a component rerendered.
---
WHO AM I: I'm Dylan, a Cloud Engineer living in Bend, Oregon. I use my background in AWS and React to make videos that enable and grow businesses.
---
🌍 My website / blog -
dylanalbertazzi.com/

Пікірлер: 53
@maXXik1337
@maXXik1337 2 жыл бұрын
I am glad that you slowly show the flow of the code as it "bubbles" through. I press D this starts, it goes here, than jumps there, then there etc. Well done.
@dylanalbertazzi
@dylanalbertazzi 2 жыл бұрын
That's what I'm here for :)
@andremelo3857
@andremelo3857 2 жыл бұрын
I'm facing this problem in a project where I'm working. I couldn't solve this, but I finally understand how the clean up works. Thank you mate.
@dylanalbertazzi
@dylanalbertazzi 2 жыл бұрын
Excellent!
@farid9323
@farid9323 4 ай бұрын
Such a simple solution. Love it! I had to move "isCanceled" to outside the useEffect hook because I had strict mode enabled.
@kikevanegazz325
@kikevanegazz325 2 жыл бұрын
Kudos, Dylan. I tried out your code and it worked just fine. Thanks a lot.
@dylanalbertazzi
@dylanalbertazzi 2 жыл бұрын
I'm so glad it helped!
@matthewbeardsley7004
@matthewbeardsley7004 Жыл бұрын
Nice video, not sure it's my problem, but I learnt something none-the-less...had no idea the cleanup gets called the next time the useEffect is called, thanks!
@ericpaul4672
@ericpaul4672 2 жыл бұрын
Quick n concise love the videos !
@dylanalbertazzi
@dylanalbertazzi 2 жыл бұрын
Glad to hear it!
@raqha4575
@raqha4575 2 жыл бұрын
It fixed the doubled "mounting" but now i want to call a function when i unmount... how can i call this only once?
@fosimuboolubo8891
@fosimuboolubo8891 2 жыл бұрын
Thanks for making this simple to grab.
@dylanalbertazzi
@dylanalbertazzi 2 жыл бұрын
You bet!
@vivianadavila3051
@vivianadavila3051 2 жыл бұрын
This was so helpful, thanks!!
@dylanalbertazzi
@dylanalbertazzi 2 жыл бұрын
Glad it was helpful!
@lacascadaobregon
@lacascadaobregon 2 жыл бұрын
Great video man. Keep making videos! Maybe make into oh who you are and what you do much shorter. Then jump right into the problem statement, why it’s a problem, where it’s applicable, and then jump directly into the solution
@dylanalbertazzi
@dylanalbertazzi 2 жыл бұрын
Always appreciate the input!
@Popc1007
@Popc1007 Жыл бұрын
Thank for explaining so well ,
@j_kimotti8350
@j_kimotti8350 2 жыл бұрын
This helps a lot! Thank you!
@dylanalbertazzi
@dylanalbertazzi 2 жыл бұрын
Glad to hear it!
@IvoTsochev
@IvoTsochev Жыл бұрын
great example mate 👍
@DevOpsDirective
@DevOpsDirective 3 жыл бұрын
"Ohhhhh... Why are we getting red dots?!" I feel that one 😂
@dylanalbertazzi
@dylanalbertazzi 3 жыл бұрын
Good to hear from ya, it's been a blast watching your channel grow!
@bananas_5856
@bananas_5856 Ай бұрын
Shit man youre giving the answer on my problem that ive been struggling within hours, great video... thanks
@RizaHariati
@RizaHariati 2 жыл бұрын
Thank you! very useful...
@dylanalbertazzi
@dylanalbertazzi 2 жыл бұрын
You are welcome!
@rohit87
@rohit87 2 жыл бұрын
Awesome. More videos on reactJs ... 🎊
@dylanalbertazzi
@dylanalbertazzi 2 жыл бұрын
More to come!
@serioussemih
@serioussemih 2 жыл бұрын
This helped me a lot
@dylanalbertazzi
@dylanalbertazzi 2 жыл бұрын
I'm glad it was helpful!
@imammahadi352
@imammahadi352 3 жыл бұрын
You can use useDebounce hook instead all of these stuff.
@dylanalbertazzi
@dylanalbertazzi 3 жыл бұрын
You're right. The useDebounce custom hook uses the useEffect cleanup function too.
@hasanbiyik01
@hasanbiyik01 2 жыл бұрын
I guess important thing in here is to knowing scope chain and closure.
@worldclasscode1847
@worldclasscode1847 2 жыл бұрын
Great video! :)
@dylanalbertazzi
@dylanalbertazzi 2 жыл бұрын
Glad it helped!
@mohhamadtoosi8128
@mohhamadtoosi8128 2 жыл бұрын
That's great. Thanks a lot.
@dylanalbertazzi
@dylanalbertazzi 2 жыл бұрын
Glad to help!
@thanhngo-ng3ci
@thanhngo-ng3ci 3 жыл бұрын
useEffect still runs every time we type, we just don't call the alert base on a flag, does it have any problem with performance ?
@dylanalbertazzi
@dylanalbertazzi 3 жыл бұрын
Just checking the flag won't have any effect on performance. However you can do more compute intensive operations inside a useEffect that you do need to be aware of performance for.
@Sam-hu3xt
@Sam-hu3xt 2 жыл бұрын
@@dylanalbertazzi in your example you should use useRef to capture the reference of the timer and invalidate it with clearTimeout. The result is more concise.
@huynguyenquoc3692
@huynguyenquoc3692 2 жыл бұрын
Could u tell me the name of vscode theme, plz
@dylanalbertazzi
@dylanalbertazzi 2 жыл бұрын
One Dark Pro! And my terminal is custom editing my .bashrc script
@ppgg997
@ppgg997 2 жыл бұрын
I thought that useffect wouldn't keep the isCancelled variable on return() and that it would re-initialise it on 2nd call again (as would happen on topical variable on a normal function 2nd call) from the beggining, thanks to showing me how begginer i am on React:P
@Sv-ry9lj
@Sv-ry9lj Жыл бұрын
Actually it will reinitialise on the 2nd time the useEffect runs. It is all due to closure where the handlechange function points to isCancelled which is modified by the return function of useEffect, so its all happening within the useEffect function of the nth time
@jasonjasonjasonjasonjason
@jasonjasonjasonjasonjason 2 жыл бұрын
I hope you have a great day 😊
@dylanalbertazzi
@dylanalbertazzi 2 жыл бұрын
I did, thanks. Right back at ya!
@akshayshinde2403
@akshayshinde2403 2 жыл бұрын
It's more like debouncing.
@boot-strapper
@boot-strapper 2 жыл бұрын
Look at that, there ARE other engineers in Bend OR
@dylanalbertazzi
@dylanalbertazzi 2 жыл бұрын
I've been waiting for this day!
@PutlerXLO
@PutlerXLO 2 жыл бұрын
and this is an antipattern...
@Rulito2405
@Rulito2405 2 жыл бұрын
Do you care to explain why it's an antipattern?
@PutlerXLO
@PutlerXLO 2 жыл бұрын
@@Rulito2405 if it is not clear, read about "React isMounted antipattern" that has been known for more than 7 years. As a result, the React maintainers decided to remove this warning in the new version, as most users do it wrong and pointless for years.
@rodrigovazquez1954
@rodrigovazquez1954 2 жыл бұрын
This is called "race condition" right?
Common React Mistakes: useEffect - Part 1
19:57
Jack Herrington
Рет қаралды 45 М.
Goodbye, useEffect - David Khourshid
29:59
BeJS
Рет қаралды 496 М.
Каха и суп
00:39
К-Media
Рет қаралды 6 МЛН
تجربة أغرب توصيلة شحن ضد القطع تماما
00:56
صدام العزي
Рет қаралды 59 МЛН
Clown takes blame for missing candy 🍬🤣 #shorts
00:49
Yoeslan
Рет қаралды 39 МЛН
All useEffect Mistakes Every Junior React Developer Makes
22:23
How To Build Your First Website For A Client - Workflow and Tools
5:39
Dylan Albertazzi
Рет қаралды 92 М.
Learn useEffect In 13 Minutes
13:38
Web Dev Simplified
Рет қаралды 823 М.
Stop Doing this as a React Developer
12:27
CoderOne
Рет қаралды 161 М.
Why I Quit Machine Learning
8:55
Dylan Albertazzi
Рет қаралды 3,3 М.
10 React Antipatterns to Avoid - Code This, Not That!
8:55
Fireship
Рет қаралды 728 М.
Understanding React's UI Rendering Process
29:07
CrossComm, Inc.
Рет қаралды 210 М.
React 18's New State Hook You've Never Heard About
22:11
Jack Herrington
Рет қаралды 87 М.
This is the Only Right Way to Write React clean-code - SOLID
18:23
ГОСЗАКУПОЧНЫЙ ПК за 10 тысяч рублей
36:28
Ремонтяш
Рет қаралды 567 М.
Лазер против камеры смартфона
1:01
NEWTONLABS
Рет қаралды 609 М.
Look, this is the 97th generation of the phone?
0:13
Edcers
Рет қаралды 5 МЛН
Сколько реально стоит ПК Величайшего?
0:37