stop useEffect react hook re-render multiple times with async call - tutorial - useEffect cleanup.

Поделиться
HTML-код
  • Опубликовано: 30 янв 2025
  • НаукаНаука

Комментарии • 53

  • @maXXik1337
    @maXXik1337 2 года назад +9

    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.

  • @andremelo3857
    @andremelo3857 3 года назад +8

    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.

  • @farid9323
    @farid9323 11 месяцев назад

    Such a simple solution. Love it! I had to move "isCanceled" to outside the useEffect hook because I had strict mode enabled.

  • @matthewbeardsley7004
    @matthewbeardsley7004 Год назад +2

    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!

  • @kikevanegazz325
    @kikevanegazz325 2 года назад +1

    Kudos, Dylan. I tried out your code and it worked just fine. Thanks a lot.

  • @raqha4575
    @raqha4575 2 года назад +1

    It fixed the doubled "mounting" but now i want to call a function when i unmount... how can i call this only once?

  • @ericpaul4672
    @ericpaul4672 2 года назад +2

    Quick n concise love the videos !

  • @DevOpsDirective
    @DevOpsDirective 3 года назад +2

    "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!

  • @lacascadaobregon
    @lacascadaobregon 2 года назад +1

    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

  • @IvoTsochev
    @IvoTsochev 2 года назад +1

    great example mate 👍

  • @bananas_5856
    @bananas_5856 7 месяцев назад

    Shit man youre giving the answer on my problem that ive been struggling within hours, great video... thanks

  • @fosimuboolubo8891
    @fosimuboolubo8891 2 года назад +1

    Thanks for making this simple to grab.

  • @rohit87
    @rohit87 2 года назад +1

    Awesome. More videos on reactJs ... 🎊

  • @Popc1007
    @Popc1007 Год назад

    Thank for explaining so well ,

  • @thanhngo-ng3ci
    @thanhngo-ng3ci 3 года назад +1

    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 3 года назад

      @@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.

  • @j_kimotti8350
    @j_kimotti8350 2 года назад +1

    This helps a lot! Thank you!

  • @imammahadi352
    @imammahadi352 3 года назад +4

    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.

  • @vivianadavila3051
    @vivianadavila3051 3 года назад +1

    This was so helpful, thanks!!

  • @worldclasscode1847
    @worldclasscode1847 2 года назад +1

    Great video! :)

  • @hasanbiyik01
    @hasanbiyik01 3 года назад +2

    I guess important thing in here is to knowing scope chain and closure.

  • @huynguyenquoc3692
    @huynguyenquoc3692 3 года назад +1

    Could u tell me the name of vscode theme, plz

    • @dylanalbertazzi
      @dylanalbertazzi  3 года назад +1

      One Dark Pro!
      And my terminal is custom editing my .bashrc script

  • @serioussemih
    @serioussemih 3 года назад +1

    This helped me a lot

  • @RizaHariati
    @RizaHariati 3 года назад +1

    Thank you! very useful...

  • @Muhammed81-u7u
    @Muhammed81-u7u 2 года назад +1

    That's great. Thanks a lot.

  • @jasonjasonjasonjasonjason
    @jasonjasonjasonjasonjason 2 года назад

    I hope you have a great day 😊

  • @ppgg997
    @ppgg997 2 года назад +1

    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 2 года назад

      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

  • @akshayshinde2403
    @akshayshinde2403 2 года назад +1

    It's more like debouncing.

  • @boot-strapper
    @boot-strapper 3 года назад

    Look at that, there ARE other engineers in Bend OR

  • @PutlerXLO
    @PutlerXLO 3 года назад +1

    and this is an antipattern...

    • @Rulito2405
      @Rulito2405 3 года назад

      Do you care to explain why it's an antipattern?

    • @PutlerXLO
      @PutlerXLO 3 года назад +1

      @@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?