A Quick Intro to Suspense in React 18

Sdílet
Vložit
  • čas přidán 3. 01. 2022
  • Learn the basics of how to work with Suspense in React 18!
    CodeSandbox: codesandbox.io/s/github/samse...
    GitHub: github.com/samselikoff/2021-1...
    (Note: As of this recording Data Fetching with Suspense is not fully supported.)
  • Zábava

Komentáře • 71

  • @mryechkin
    @mryechkin Před 2 lety +39

    Killer video! This is by far the most digestible demo of Suspense that I've seen

  • @RandomGuy1606
    @RandomGuy1606 Před 2 lety +23

    Great demo for the concept. Call me crazy but the UX before suspense was added is better in this example IMO. render the static content immediately and then fill in the dynamic data. Otherwise you have way to much movement on the page waiting for data to be loaded before rendering the component

    • @samselikoff
      @samselikoff  Před 2 lety +9

      Definitely a valid argument! You can opt to build in these sort of skeleton screens as you like throughout your app. But personally I find that they introduce "UI anxiety" because they give the user too many things to look at. I prefer at most one intermediate screen if an app is transitioning from a pending to a loaded state, because it minimizes how much the user has to "guess" until the app is ready. I find most (all?) native iOS apps behave this way.

  • @JoelGrayson
    @JoelGrayson Před rokem +1

    Came here for suspense. Ended up learning about useSWR, emmet wrap with abbreviation, and framer-motion. Great video!

  • @sharmanultra2756
    @sharmanultra2756 Před 2 lety +6

    This is awesome. I love how you show off the power of Suspense in such a simple way!

  • @danielbark
    @danielbark Před 2 lety +3

    Thanks Sam! Really love the clean and informative style you have. Inspiring!

  • @JustSkillGG
    @JustSkillGG Před rokem

    Sam your explaining skills are from another level.
    Thanks for the awesome videos.
    Subscribed

  • @doomguy2135
    @doomguy2135 Před rokem

    So glad I discovered this channel!

  • @chibuikemmonu3691
    @chibuikemmonu3691 Před 3 měsíci

    Absolutely loved your explanation

  • @tech3425
    @tech3425 Před rokem

    Something about the way you explain, dude. Makes it sound really easy to understand.!

  • @rity456789
    @rity456789 Před 2 lety +1

    This is awesome but simple, thank you so much!

  • @ajithtina
    @ajithtina Před rokem

    Thanks Sam! Very helpful video!

  • @vonderklaas
    @vonderklaas Před rokem

    Thank you very much, very good quality and content
    You got yourself a new subscriber!

  • @ruuman4
    @ruuman4 Před 10 měsíci

    I really enjoyed this video. Thanks for the great content!

  • @hello12345720795131
    @hello12345720795131 Před rokem

    Great video, super clear!

  • @krimod
    @krimod Před 2 lety

    Awesome video !! Thanks for the amazing content

  • @daCorasla
    @daCorasla Před 2 lety +2

    So it basically marks something as being async and doing an await at component level. This is pretty cool since it’s a very basic paradigm to wait for data fetching. You would do it the same way by working a bit with the data store and setting some flags, manually

  • @setoseng
    @setoseng Před 11 měsíci

    Just stumbled upon your channel. Super informative content and presentation. Thank you.

  • @nostalgicnow6001
    @nostalgicnow6001 Před rokem +1

    Love this!!

  • @davidtparks
    @davidtparks Před 2 lety

    Awesome video man thanks!

  • @rahulsriram6295
    @rahulsriram6295 Před 2 lety +1

    Great video! Would love to see if you make react server components videos

  • @kingfoxx6697
    @kingfoxx6697 Před 2 lety

    Awesome teaching method dude

  • @SinhNguyen-tz6us
    @SinhNguyen-tz6us Před 2 lety

    Thanks for this great video!!!

  • @boredhuman23
    @boredhuman23 Před 2 lety

    Underrated channel 😍

  • @mkvalor
    @mkvalor Před 2 lety +2

    This is cool but -- how does centralizing this in the parernt handle it if one (or more) of the child components cannot be fetched? It seems like it would just show the spinner indefinitely for the whole group -- whereas before, we would at least see the stats from the child components that did complete their fetches.

    • @samselikoff
      @samselikoff  Před 2 lety +2

      You can make it throw an error so you can use Error Boundaries to handle that case. It's actually really nice because before we couldn't use error boundaries with async code, but Suspense basically turns async code into sync code. I think I'll make a video on this because several folks have asked!

  • @muktadirhassasn269
    @muktadirhassasn269 Před 2 lety

    Awesome content.

  • @ElijahScott10x
    @ElijahScott10x Před 2 lety

    Great demo

  • @cseckler
    @cseckler Před 2 lety +3

    Great video! Explained so well! Thank you! Can you please share with us how you are wrapping your tags when you highlight them in your code? I assume it’s an extension?

    • @unmy400
      @unmy400 Před 2 lety +4

      it's emmet in vscode, ctrl+shift+p -> wrap with abbreviation. If you use it recently it will be just ctrl+shift+p -> enter, so it's pretty fast

  • @andyjones11
    @andyjones11 Před 2 lety +2

    Great video. I’d love to learn more about how (if possible) you can use different suspense boundaries depending on the render - i .e on initial page load delegate to top level suspense boundary, but if the user navigates to another page within the same app, delegate to a lower-level suspense boundary.

    • @samselikoff
      @samselikoff  Před 2 lety +1

      This is something I've wondered as well! There is currently a unstable_avoidThisFallback prop that does just this, but the team has said they don't know if it will change or even be part of the final API. I'm hacking on some demos for how to do this cleanly today, stay tuned...
      Check out the original PR for more info: github.com/facebook/react/pull/15578

  • @braveshine2579
    @braveshine2579 Před rokem +2

    1:38 does anyone know how he pulled the trick to warp the div? or what ext did he use?

  • @seanfang398
    @seanfang398 Před 2 lety +2

    Wow, really love the style of this video, so clean and so infomative, subscirbed already❤

  • @sundarrajendiran2722
    @sundarrajendiran2722 Před 11 měsíci

    This is awesome

  • @justine_chang39
    @justine_chang39 Před rokem

    awesome video

  • @Sm123bg
    @Sm123bg Před 2 lety

    I’m hoping you can help clear up something I’ve struggled with for years. I see that you have a component for each platform where you fetching data for each. Is that the recommend approach or do you typically create a single component and pass the data in as a prop? This would require that you fetch all your data at a higher component. Looking forward to your answer! Thanks for any help.

  • @melancholy-engineering
    @melancholy-engineering Před 2 lety +1

    Thanks for this video!
    Could you please share your VSCode configurations with list of extensions?

  • @misterkilluminati6949
    @misterkilluminati6949 Před 11 měsíci

    Thank you, sir. Swift explanation.
    How should we enable this suspense option if we are using fetch API ?

  • @adeniyitaofik3832
    @adeniyitaofik3832 Před rokem

    Hello would like to know, hiw about when each component time to fetch data varies cant one render each has it has data instead of waiting for all to have data

  • @jimshtepa5423
    @jimshtepa5423 Před 2 lety +4

    if one of the children fails to fetch would it prevent others from rendering to display?

    • @samselikoff
      @samselikoff  Před 2 lety +2

      Suspense lets you use Error Boundaries for data fetching like you're used to doing for rendering errors! reactjs.org/docs/concurrent-mode-suspense.html#handling-errors

    • @jimshtepa5423
      @jimshtepa5423 Před 2 lety

      @@samselikoff thank you for a quick reply. May I ask one more question (I hope it is not getting annoying=): when you use useSWR, I noticed you didn't supply second argument (i.e. fetcher), is it because you are using miragejs? or is not specific to mirage but swr?

    • @samselikoff
      @samselikoff  Před 2 lety +4

      @@jimshtepa5423 No prob + def not annoying, love getting questions! I always use the global config via the provider to set up a single fetcher for all my API requests, you can see it in the demo here: github.com/samselikoff/2021-11-18-suspense/blob/16c41689f55d0747de06528d73cef93f89f19e16/pages/_app.js#L17-L21

    • @jimshtepa5423
      @jimshtepa5423 Před 2 lety

      @@samselikoff understood. thank you

  • @jcpaesf
    @jcpaesf Před rokem

    if i need one spinner for each component, i need wrap the suspense in each of them?

  • @NurbolNygmanov
    @NurbolNygmanov Před rokem

    Thanks for good explanation. But what if one of children throws an error or times out? all of them will time out then?

  • @braveshine2579
    @braveshine2579 Před rokem

    i downloaded the code, which api of twitter and others did you use?

  • @nileshchauhan7117
    @nileshchauhan7117 Před měsícem

    thanks bro

  • @nathanm474
    @nathanm474 Před rokem

    Will Suspense only render the page when all of the images are FULLY loaded? I've noticed a number of my images load initially fairly fast but then load in quarter increments so I'd like to use Suspense if that's the case.

    • @samselikoff
      @samselikoff  Před rokem

      The idea is you can control whether each image should block rendering or not (by rendering a fallback)

  • @StingSting844
    @StingSting844 Před 2 lety

    How does the suspense know that there is some async fetch happening down the tree? Does the useSWR hook do something that makes Suspense component aware of this?

    • @samselikoff
      @samselikoff  Před 2 lety +1

      SWR with suspense: true uses Suspense, which means it throws a promise during render if the data is not there yet. That works no matter how deep in the tree - any component that throws a promise during render will cause the whole tree to suspend until all the promises resolve, then React tries to render again.

  • @user-kr6lp7rm5y
    @user-kr6lp7rm5y Před 2 lety

    Suspense is nice to use with atomic state managers like jotai or recoil

  • @SogMosee
    @SogMosee Před rokem +1

    are you using vscode vim?

  • @hadieini4870
    @hadieini4870 Před 10 měsíci

    But what if one of the API calls fail to response? Then we'll have an invalid data.. What will suspense do then? Show the spinner forever?!

  • @JoePrivett-cr5np
    @JoePrivett-cr5np Před rokem

    Hmm, not sure I understand this sentence at 4:03 "... in Suspense this data will always be populated, if it's not...". If it's always populated, how can it not be populate :) ?

  • @shinnthantminn9758
    @shinnthantminn9758 Před 2 lety

    lazy method is does not need in suspense in react 18 ??

  • @tushargaming2530
    @tushargaming2530 Před 2 lety

    But what if all the components have loaded but one component require more time, then others have to also wait for it to load

    • @samselikoff
      @samselikoff  Před 2 lety

      You can move the Suspense boundaries around to tweak the loading behavior - check out my follow-up video to this one! czcams.com/video/h_vVsPwvcsg/video.html

  • @archmad
    @archmad Před 2 lety

    i honestly prefer before you applied the suspense. esp if there are calculations and different api latency for each call.

    • @samselikoff
      @samselikoff  Před 2 lety +1

      There are some more composability benefits with Suspense, especially around error handling. Check out this vid if you haven't yet: czcams.com/video/h_vVsPwvcsg/video.html

    • @archmad
      @archmad Před 2 lety

      @@samselikoff i am using Error Boundaries, so im good. i'd rather incrementally load what is ready than load the whole thing at once

  • @sandeepacharya1345
    @sandeepacharya1345 Před 2 lety

    what if a single stat components fails to load the data?

    • @samselikoff
      @samselikoff  Před 2 lety +1

      Suspense lets you use Error Boundaries for data fetching like you're used to doing for rendering errors! reactjs.org/docs/concurrent-mode-suspense.html#handling-errors

  • @technikhil314
    @technikhil314 Před 2 lety

    Can I have my parent handle deep nested suspended child (not just immediate child)?

    • @samselikoff
      @samselikoff  Před 2 lety +1

      Yep, any component that suspends will look up the tree for the nearest ancestor Suspense component, even if that nearest ancestor is all the way at the root!

  • @333ruka
    @333ruka Před rokem

    can I also get that spinner animation ? 😅

  • @nihithg671
    @nihithg671 Před rokem

    Bruh I thought this was a video on how to use suspense in a story -_-

  • @dhariri
    @dhariri Před 2 lety

    Love your channel and the quality of these videos. Thanks Sam!