Hook traps: UseEffect and Stale Closures

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

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

  • @Zeus-yy2dc
    @Zeus-yy2dc 2 года назад

    I was screaming STALE STATE VARIABLES all over google until I found your video, THANKS A LOT MAN !

  • @kashmirtechtv2948
    @kashmirtechtv2948 10 месяцев назад

    You are awesome. Saw your video for the 1st time

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

    You talk like an authority on the subject because you are. I find your videos very insightful, thank you.

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

    I didn't run into this kind of problem, yet. Thanks for this valuable info.

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

    Nice video! An alternative approach worth mentioning which I've seen is storing the timeout in a ref and checking if one exists every time the useEffect callback runs. That allows setting the interval in a function that might be triggered by an onClick handler.

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

      Yes, that also a good idea, and it would work.

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

    Great explanation. I faced this stale closure problem and this video was very helpful.

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

    Great video, explained clearly. useEffect is very useful but can also be tricky. Thank you for explaining it so clearly.

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

    Very good explanation, thanks 👏👏

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

    Very helpful as always!

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

    thank you, brilliant explanation, loved your book as well!

  • @梁永霖-o6u
    @梁永霖-o6u 3 года назад +1

    Great work, but how about the third situation which is without the dependency array in useEffect and what kinda situation we have to use it?

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

      You could also use a ref there.

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

    very good explaination, thanks mate!

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

    Thanks, keep up the good work!

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

    Very helpful and well explained.
    Didn't fix the first useEffect in the end, tho.

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

    This was helpful
    Thanks a lot

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

    I have a question regarding stale closures. I'm interested in a scenario where you subscribe the component to a notification service that get's called from a socket message. So you subscribe to that service using useEffect with [], and you pass a callback that the notification service will call when he has an update. How can you avoid stale closures on this scenario. Thanks!

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

      I think that you would have to do like my event example: add all the dependencies to the useEffect array and unsubscribe/resubscribe on changes.

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

    Good experience 👍

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

    This is brilliant

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

    I haven't watched the whole video, but hopefully eslint-plugin-react-hooks is mentioned as a way to never have to face this issue, ever

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

      Since you’re wondering: yes, at the end of the video.

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

    Thanks man

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

    thanks for idea. Maybe something simple we can make?
    useEffect(() => {
    const intervalId = setInterval(() => {
    setTimer((t) => t + 1);
    }, 1000);
    const timerId = setTimeout(() => {
    const wpm = inp.current.value.split(" ").length;
    alert(wpm);
    clearInterval(intervalId);
    clearInterval(timerId);
    }, 5000);
    }, []);
    return (
    )

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

    interesting...