React 18 useTransition Hook Crash Course

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 ม.ค. 2025

ความคิดเห็น •

  • @marindedic441
    @marindedic441 2 ปีที่แล้ว +192

    React really needs to start naming their hooks better

    • @ChrisAthanas
      @ChrisAthanas 2 ปีที่แล้ว +15

      Yes I was thinking it should be called useLowPriority or useDeferredPriority or useInterruptable

    • @adityaanuragi6916
      @adityaanuragi6916 ปีที่แล้ว +3

      Ikr even the startTransition doesn't make a lot of sense to me. I would've preferred a name like lowPriority or something

    • @Toulkun
      @Toulkun ปีที่แล้ว +39

      I thought transitions were about those ordinary transitions with ease, ease out, ease in out 😂

    • @dreamyrhodes
      @dreamyrhodes ปีที่แล้ว +1

      This! Although refactoring might break a lot now. But it is really bad especially for beginners but also for developers that don't develop React all the time but come back to it for a new need once in a while.

    • @royhyde8842
      @royhyde8842 ปีที่แล้ว +2

      Tell me about it. You could never tell by the hook's name that that is what it does.

  • @kalideb-y3y
    @kalideb-y3y 26 วันที่ผ่านมา +1

    I understood this explanation more than the actual documentation. Lesson learned: it's not always that reading documentation saves you time, sometimes watching videos demystifying documentation is best. 😅

  • @timj7785
    @timj7785 ปีที่แล้ว +7

    this feels like a good use case for debounce as well, thanks for sharing, you made it super clear!

  • @ADITYA-qr6il
    @ADITYA-qr6il 2 ปีที่แล้ว +18

    You explain a useTraction in a very simple way 🤯

    • @codesymphony
      @codesymphony 2 ปีที่แล้ว +4

      my favourite hook useTraction

    • @Linuslkm
      @Linuslkm ปีที่แล้ว +2

      @@codesymphony your comment made me laugh thanks lol

    • @_KITISH
      @_KITISH ปีที่แล้ว +1

      @@Linuslkm lol yeah 😂

  • @jasonleelawlight
    @jasonleelawlight 7 วันที่ผ่านมา

    Thank you for this great video! It's so much easier to follow than the official docs, basically the gist is we can give different priorities to different state changes when it comes to updating the UI.

  • @elijahandrew6790
    @elijahandrew6790 2 ปีที่แล้ว +14

    The best programming tutor out there.
    I'm very careful with words I use to describe people (words like "best", "most awesome" etc), and I rarely ever use them.
    But...Damn! Kyle, You're the BEST.
    Thanks for helping me become a much better programmer.
    Lots of love from Nigeria

  • @rvft
    @rvft 2 ปีที่แล้ว +3

    I literally opened TH-cam to see if WDS posted a video and he did lol

  • @23LS023
    @23LS023 2 ปีที่แล้ว +4

    Your channel is the best, never met a person, who can explain any difficult topic so easily, that even a non-English speakers can understand every single detail. Thank you very much ...

  • @tomino133
    @tomino133 2 ปีที่แล้ว +18

    You're an absolute legend. Thanks for a nice and clear explanation.

  • @pcr3323
    @pcr3323 2 ปีที่แล้ว +23

    Sir, I don't know whether you'll read My comments, But THANKS ALOTTTT ❤️.
    I Got My developer job mostly because of your tutorials. And I'm so grateful for this sir. God bless you. I wish I could meet you in real and thank you ❤️...
    Really thank you for making web development simple 🥰 ...
    I'm a commerce graduate and taught myself coding .. and mostly your tutorials is where I refer to .. and the project I did to land my job wouldn't have been possible without your help..
    Again THANKS ALOT ❤️🙏

  • @gravisrinivas
    @gravisrinivas 2 ปีที่แล้ว

    You are the best react tutor on youtube

  • @ShaharHarshuv
    @ShaharHarshuv ปีที่แล้ว

    What's amazing is that the delay is CPU related, it's synchronous. But when you do that you still manage to update the UI to "loading" before the thread is stuck very easily. Amazing

  • @TusharJade101
    @TusharJade101 2 ปีที่แล้ว +1

    Your video are very easy to understand man lot of love from India ❤️

  • @tech3425
    @tech3425 ปีที่แล้ว

    Had a tough time wrapping my head around this hook. Your video really simplified it a LOT. The key takeaway was that all `set` events are high priority by default, and the `useTransition` marks something as low priority instead.

  • @romanjs4909
    @romanjs4909 ปีที่แล้ว

    amazing explanation! Subscribed

  • @kantyDarius
    @kantyDarius 2 ปีที่แล้ว +1

    This hooks is so helpful.. Thanks Kyle... Cheers from Argentina.

  • @leah9227
    @leah9227 2 ปีที่แล้ว +1

    This is kind of an async function with a isPending loading flag. Awesome, thanks for the information!

  • @MohamadOmar-oi3ur
    @MohamadOmar-oi3ur 2 ปีที่แล้ว +2

    YOU MAN are a life saver !!!
    I really enjoy watching your videos even i already have background about what you are talking about , but at the same time you guided me and gave me a lot of solution about some old and new problems !
    I appreciate it Bro

  • @95jugal
    @95jugal 2 ปีที่แล้ว +6

    Love your content ❣️. Please make more videos on other features of React 18 too.

  • @SushilKumar-ig8ls
    @SushilKumar-ig8ls 2 ปีที่แล้ว +1

    Totally Loved it. Thank you for such new concepts

  • @bonkshiba1916
    @bonkshiba1916 2 ปีที่แล้ว +1

    Nice and clear explanation, thanks.

  • @Yadiel458x
    @Yadiel458x 8 หลายเดือนก่อน

    It just clicked! Very good explanation!

  • @lindawang3008
    @lindawang3008 ปีที่แล้ว

    Thank you for making this amazing video! Easy to follow! Get the idea about "useTransansition" in just 7 mins! 😀

  • @zhesun4853
    @zhesun4853 2 ปีที่แล้ว +1

    really love this guy, get knowledge explained so clear! thumbs up!👍

  • @NicoKupfer
    @NicoKupfer ปีที่แล้ว +12

    One small note, the startTransition method also causes a render because it has to change the status to isPending=true. so in the end it’s 3 renders vs 1.

    • @dreamyrhodes
      @dreamyrhodes ปีที่แล้ว

      But only if you use isPending in your JSX, not?

    • @bigti1199
      @bigti1199 ปีที่แล้ว

      It changes the state of pending but it still does so in the same render it changes the state of the array
      So it does all that in 1 render

    • @NicoKupfer
      @NicoKupfer ปีที่แล้ว +2

      @@bigti1199 6:50 - startTransition runs asynchronously (i'm talking about updating the state). It doesn't matter in terms of performance because of the amount of resources that useTransition saves makes it all worthwhile. But it does render three times. Here's why:
      Without startTransition, it will do *synchronously* (although your computer will be really suffering for 3 seconds) one update (setInput() and setList()).
      With startTransition, it does it three times:
      - setInput
      - (setTransition gets delayed) -> sets `isPending` to true.
      - (setTransition code runs) -> setList
      - setTransition Ends, and when it ends (because it doesn't know what happens inside of it) sets `isPending` to false. Since this is an asynchronous operation, it cannot change `isPending` to false on the same rerender as `setList(l)`.
      The last point, which is the one i was referring to, is due to a state being updated inside of startTransition. This is something that Facebook knows, and they have a warning in case they detect multiple re-renders while running the code. "'Otherwise concurrent mode guarantees are off the table.'"
      Feel free to check the ReactStartTransition.js file of the source code :)

  • @kranthikumar5215
    @kranthikumar5215 ปีที่แล้ว +1

    Love u man❤

  • @SyamKishoreNaidu
    @SyamKishoreNaidu 2 ปีที่แล้ว

    Thanks

  • @lakshyapandit6624
    @lakshyapandit6624 2 ปีที่แล้ว

    This hook is a life changer. Thanks bro..

  • @iivnnal7483
    @iivnnal7483 2 ปีที่แล้ว

    CONGRATULATIONS! 🎉

  • @fiqriardiansyah876
    @fiqriardiansyah876 2 ปีที่แล้ว +11

    so basically it's like web workers who working at background thread

  • @erezrosenbloom330
    @erezrosenbloom330 2 ปีที่แล้ว

    This was an excellent overview. Thanks!

  • @LePhenixGD
    @LePhenixGD ปีที่แล้ว

    Damn, didn't even know you could use this in React! That's amazing!

  • @VondiaLC
    @VondiaLC 2 ปีที่แล้ว

    Love your content! Thanks man keep up the good work

  • @EvertJunior
    @EvertJunior 2 ปีที่แล้ว

    Thanks Kyle, very informative ! Really good hook

  • @anatolydyatlov963
    @anatolydyatlov963 2 ปีที่แล้ว

    Nice! I didn't know about this one.

  • @maranaivoson3199
    @maranaivoson3199 2 ปีที่แล้ว +1

    Very clear, thank you Kyle👍

  • @DiariesByMin
    @DiariesByMin 2 ปีที่แล้ว

    Absolutely simple and clear THANKS

  • @Kennomie
    @Kennomie 2 ปีที่แล้ว

    Really nice explanation, thank you!

  • @mdridoy-ef2pw
    @mdridoy-ef2pw 2 ปีที่แล้ว +6

    when someone types 5 letters, the startTransition code block also fires 5 times.
    It would be nice if they added option to only fire the startTransition code block for the last input.
    just like how a debounce function works.

    • @Vesunchik123456789
      @Vesunchik123456789 2 ปีที่แล้ว +3

      usedeferredvalue is also a new hook which can solve that problem

    • @334Bena
      @334Bena 2 ปีที่แล้ว +2

      You could probably set it back with a useEffect hook using a setTimeout that waits for the user to stop typing in order to actually set the new state

    • @allone258
      @allone258 2 ปีที่แล้ว

      better use timeout, with 0.5 sec after input.
      if user types again before 0.5sec, reset the timeout.

  • @yfzhangphonn
    @yfzhangphonn ปีที่แล้ว

    Cure for INP, nice hook and thanks for sharing.

  • @programmingwiththotho4641
    @programmingwiththotho4641 4 หลายเดือนก่อน

    Thank You , for sharing the knowledge

  • @dlysele
    @dlysele 2 ปีที่แล้ว

    Very easy to understand. Thank you.

  • @shinodinhaa
    @shinodinhaa 2 ปีที่แล้ว +2

    cool, will let me stop adding lodash to my projects only to use their debounce function hahaha

  • @5906646
    @5906646 หลายเดือนก่อน

    great explanation. Thanks a lot

  • @moaazbhnas886
    @moaazbhnas886 2 ปีที่แล้ว

    Awesome explanation!

  • @TroenderTass
    @TroenderTass 2 ปีที่แล้ว +1

    Sound to me the concept is explained wrong. The rendering merging might be correct, but the only reason why this blocks, is that non of the async state change calls will execute until the current function exit. That is the normal eventloop in js. All asynchronous functions will be put in a que that will exectue when the stack is cleared. So the setTransition is just another async function that wont block, but alow the handler function to exit.

  • @ignaspan
    @ignaspan 2 ปีที่แล้ว +3

    7:00 isn’t it 3 separate renders: setInput, isPending change, setList?

    • @mob_abominator1868
      @mob_abominator1868 2 ปีที่แล้ว

      Wouldn't that be part of the same batch update ? First SetInput and isPending is included in the first render, then setList and isPending is included in the second render. Though I am not 100% sure on this.

  • @krishangopal2475
    @krishangopal2475 2 ปีที่แล้ว +23

    Lets say input changes by 1 character and code is called inside startTransition which is still being calculated in the background. When u type some more characters which call that handleChange everytime u type a character, Does that code inside startTransition gets cancelled whenever it runs setInput again as handleChange have been called multiple times?

    • @michaelkurzewski2937
      @michaelkurzewski2937 2 ปีที่แล้ว +2

      I think it's fairly easy to check. Console log max index of each iteration. If the index same for each iteration (equal to items count) then it's not cancelled.

    • @awekeningbro1207
      @awekeningbro1207 2 ปีที่แล้ว +6

      That's the debounce functionality. You have to do implement that yourself.

    • @Mauro0
      @Mauro0 2 ปีที่แล้ว +1

      Yes, it does run every time. If you have, let's say, a search input and want to avoid making a request for every character, then you should implement a Debounce().

    • @Grim_tidings
      @Grim_tidings 2 ปีที่แล้ว +3

      Yes. However, if you’re using lodash, there’s a useDebounce hook included that can alleviate this issue. If not, implementing it isn’t hard:
      function useDebounce(value, delay) {
      const [debouncedValue, setDebouncedValue] = useState(value);
      useEffect(
      () => {
      const handler = setTimeout(() => {
      setDebouncedValue(value);
      }, delay);
      return () => {
      clearTimeout(handler);
      };
      },
      [value, delay]
      );
      return debouncedValue;
      };

  • @RafaGallego729
    @RafaGallego729 หลายเดือนก่อน

    Thanks men, you're great

  • @DEV_XO
    @DEV_XO 2 ปีที่แล้ว

    Amazing mate, thanks!

  • @tornikenijaradze6422
    @tornikenijaradze6422 2 ปีที่แล้ว

    Thanks for the video!

  • @ayaalnahas3577
    @ayaalnahas3577 2 ปีที่แล้ว

    Great tutorial !! THANKS!

  • @bayasahamed1815
    @bayasahamed1815 ปีที่แล้ว

    Thanks for the clear explanation!! :-)

  • @Victor_Marius
    @Victor_Marius 2 ปีที่แล้ว

    Super condensed: setList(Array(LIST_SIZE).fill(e.target.value)

  • @irlshrek
    @irlshrek 2 ปีที่แล้ว

    Man.. This makes me appreciate Svelte so much

    • @danieltkach2330
      @danieltkach2330 2 ปีที่แล้ว +1

      How is this handled in Svelte?

    • @kevinc4736
      @kevinc4736 2 ปีที่แล้ว

      ^

    • @krishgarg2806
      @krishgarg2806 2 ปีที่แล้ว

      I mean I did this in svelte and it was sluggish so what are you talking about?
      let val = "";
      let list = [];

      const LIST_SIZE = 20000;

      const updateList = (val) => {
      list = []
      for (let i = 0; i < LIST_SIZE; i++) {
      list = [...list, val]
      }
      }
      $: updateList(val)

      {#each list as item, idx (idx)}

      {item}

      {/each}

    • @krishgarg2806
      @krishgarg2806 2 ปีที่แล้ว

      Update: Added debouncing and works much better:
      let val = "";
      let list = [];

      const LIST_SIZE = 20000;

      const debounce = (func, time = 1000) => {
      let timeoutId = null;
      return (...args) => {
      if (timeoutId) {
      console.log(`Cleared timeout with id: ${timeoutId}`)
      clearTimeout(timeoutId);
      }
      timeoutId = setTimeout(() => {
      console.log(`Called timeout callback with id: ${timeoutId}`)
      func(...args)
      }, time)
      }
      }
      const updateList = debounce((val) => {
      list = []
      if (!val) return;
      for (let i = 0; i < LIST_SIZE; i++) {
      list = [...list, val]
      }
      });
      $: updateList(val)

      {#each list as item, idx (idx)}

      {item}

      {/each}

  • @ControlAllDa1337
    @ControlAllDa1337 2 ปีที่แล้ว +1

    Thanks my guy, this looks like it could be useful for me atm, I need to filter through a list of categories as the user types. The list is small atm but could potentially become very large

  • @dilanbesong1877
    @dilanbesong1877 2 ปีที่แล้ว

    Thank you Kyle for helping me grow in development, please can you make a video on how to automatically delete documents in mongodb

  • @code-for-mars
    @code-for-mars 2 ปีที่แล้ว

    Best explanation !!

  • @Kyle-NP
    @Kyle-NP ปีที่แล้ว

    Anyone know how he quick moved all the below lines into the arrow func inside startTransition @ 4:22?

  • @VladDubovyi
    @VladDubovyi 2 ปีที่แล้ว +4

    Can you review all of the hooks and react18 updates?

    • @krishangopal2475
      @krishangopal2475 2 ปีที่แล้ว +6

      React 18 updates are useTransition hook that you already saw which is a concurrent mode feature that React team has been working on for years. And another update is useId hook that you can generally use to generate unique ids in your application

  • @nicklane4868
    @nicklane4868 2 ปีที่แล้ว

    Amazing video, thanks.

  • @danish_ayoub
    @danish_ayoub 2 ปีที่แล้ว

    Very useful and well explained

  • @bidishadas832
    @bidishadas832 2 ปีที่แล้ว

    Awesome explanation.

  • @electricindro2236
    @electricindro2236 2 ปีที่แล้ว

    Thanks! Nice explanation 🔥

  • @yazraf
    @yazraf 2 ปีที่แล้ว

    Dude you are awesome!

  • @essaadi_elmehdi6784
    @essaadi_elmehdi6784 2 ปีที่แล้ว

    But the loop will execute inside handleChange. So the useTranslition will help us just on render the list after input have been rendered (so rendering the input will be more fast because thet list wont render with it)

  • @omarbailal5751
    @omarbailal5751 2 ปีที่แล้ว

    Great content, thank you sir.

  • @Kingofmylife007
    @Kingofmylife007 2 ปีที่แล้ว +1

    that's great usefull.
    where is useId and another hooks.

  • @CreativeB34ST
    @CreativeB34ST ปีที่แล้ว

    How do you do that line swapping at 4:23?

  • @Gitaadiguna
    @Gitaadiguna 2 ปีที่แล้ว

    Lol this is the exact answer my app's issue looking for

  • @slowbr600rr
    @slowbr600rr 2 ปีที่แล้ว

    Congratulations 😉

  • @orkhanrustamli2039
    @orkhanrustamli2039 2 ปีที่แล้ว

    Hey, great content as always!!! Could you please make a video about Kafka usage with NodeJS. I am really interested on some advanced topics such as manual commit, exact once delivery, concurrency and etc!

  • @roshanlalsahu7846
    @roshanlalsahu7846 2 ปีที่แล้ว +1

    what is the execution order in case of async function?

  • @mealcode
    @mealcode 2 ปีที่แล้ว

    before useTransition, i used debounce to make similar functionality working.

  • @onurtekingundogdu9568
    @onurtekingundogdu9568 ปีที่แล้ว +1

    I m not sure what did I wrong.. Before I start I created a fresh project, I checked that react V. was 18. tried to write the same code but could not see the same changes on my screen.
    Let me paste the code piece
    const LIST_SIZE = 2000000;
    function handleChange(e: React.ChangeEvent) {
    setInput(e.target.value);
    startTransition(() => {
    const l = [];
    for (let i = 0; i < LIST_SIZE; i++) {
    l.push(e.target.value);
    }
    setList(l);
    });
    }

  • @VaibhavShewale
    @VaibhavShewale 2 ปีที่แล้ว +1

    just use another useState and make it a priority!
    i like it

  • @AadeshKulkarni
    @AadeshKulkarni 2 ปีที่แล้ว

    Beautiful!

  • @eileen0307
    @eileen0307 2 ปีที่แล้ว +1

    could you record a video to talk about nextjs?

  • @tangkenangenamotv6401
    @tangkenangenamotv6401 2 ปีที่แล้ว +1

    Good video as always, man! Thank you!!! But what's the difference between useTransition and useDebounce?

    • @zai8440
      @zai8440 2 ปีที่แล้ว

      same question, since both of these will be used on maybe the same use cases

  • @Kim-by5uy
    @Kim-by5uy ปีที่แล้ว

    i feel isPending should've been the second value. Because you wont always need it, which will cause an 'unused variable' error with typescript

  • @elliotsharpe5448
    @elliotsharpe5448 ปีที่แล้ว

    Whats the difference here between using useTransition in the handleChange function and having a useEffect which is has this code separately which triggers when the input changes?

  • @portusdelphini
    @portusdelphini ปีที่แล้ว

    This always being called threshold thru ages.

  • @wodyd318
    @wodyd318 2 ปีที่แล้ว

    thank you

  • @malekessam6443
    @malekessam6443 4 หลายเดือนก่อน

    so, in this code example the useTransition hook is used to prevent the loading state of the suspense fallback as the goal is keeping the original UI as it is until the lazy loaded component finishes loading. The problem is that I do not understand how this code achieves that goal. The use transition will try to delay the setAdmin as much as possible but the lazy loading will not start until the admin state is true as it will be triggered by the conditional rendering. So, from what i understand this shouldn't do anything and the loading state should occur normally but thats not correct and I do not know why.

  • @alanmauriciocarrascoperez2188
    @alanmauriciocarrascoperez2188 2 ปีที่แล้ว

    so... a good use case could be a search input with fetch API?

  • @FilipCodes
    @FilipCodes 2 ปีที่แล้ว +2

    Thats all nice and fun until state changes do not depend on each other

  • @wasimnadaf11
    @wasimnadaf11 2 ปีที่แล้ว

    Great video

  • @firewatermoonsun
    @firewatermoonsun ปีที่แล้ว

    useDeferredValue() also sets to lower priority.

  • @amershboul9107
    @amershboul9107 ปีที่แล้ว

    Very Useful

  • @arnoderoost3479
    @arnoderoost3479 2 ปีที่แล้ว

    Thank you for the insights. Any ideas on how to use this in Next js?

  • @AmodeusR
    @AmodeusR 2 หลายเดือนก่อน

    It'd be good to update this video since React 19 brought some relevant changes to this hook.

  • @usd00
    @usd00 2 ปีที่แล้ว

    Great.
    btw why don't you join the copilot waitlist?

  • @morgandorferr
    @morgandorferr 2 ปีที่แล้ว

    Kyle is great 👍🏻

  • @nguyenngoctuan1616
    @nguyenngoctuan1616 ปีที่แล้ว

    Can it do one more better if I would like to stop a running transition?

  • @agungprasetyaaajati4527
    @agungprasetyaaajati4527 2 ปีที่แล้ว

    It's possible to use useTransition for fetching api data? Could you give me example what purpose of useTransition is?

  • @wellingtonostemberg7585
    @wellingtonostemberg7585 2 ปีที่แล้ว +1

    Nice content

  • @dawid_dahl
    @dawid_dahl 2 ปีที่แล้ว

    Thank you!

  • @abdelheqbourouni3677
    @abdelheqbourouni3677 2 ปีที่แล้ว

    In your example we can use flushSync !!

  • @berkaybayrak7876
    @berkaybayrak7876 2 ปีที่แล้ว

    ı cannot find any redux tutorial video on your channel. do u have a plan about any redux videos?

  • @bonnes04
    @bonnes04 2 ปีที่แล้ว

    Ni noma... respect