Write Clean Code with React Suspense | React Error Boundaries Explained

Web Dev Roadmap for Beginners (Free!): bit.ly/DaveGrayWebDevRoadmap
React Suspense is Scary Good! In this tutorial, React Suspense & React Error Boundaries are explained with examples. You will learn how to provide a fallback loading state component and a fallback error component to improve your app's user experience.
🚀 Become a full-stack web dev with Zero To Mastery Courses:
- Advanced React: bit.ly/AdvReactDev
- The Complete Web Developer: bit.ly/WebDevMaster
- Junior to Senior Dev Roadmap: bit.ly/WebDevRoadmap-JrtoSr
🚩 Subscribe ➜ bit.ly/3nGHmNn
📬 Course Updates ➜ courses.davegray.codes/
❓ Questions - Please post them to my Discord ➜ / discord
☕ Buy Me A Coffee ➜ www.buymeacoffee.com/davegray
👇 Follow Me On Social Media:
Github: github.com/gitdagray
Twitter: / yesdavidgray
LinkedIn: / davidagray
🔗 Source Code: github.com/gitdagray/react-su...
React Suspense & React Error Boundaries with SWR
(00:00) Intro
(00:25) Welcome
(00:48) Starter Code & Start up
(02:26) SWR Data Fetching
(05:02) Enable Suspense in SWR
(05:29) Conditional logic to replace
(06:27) Adding React Suspense
(12:16) PostsList component clean up
(13:10) Why We Need Error Boundaries
(14:05) Install react-error-boundary
(15:06) Adding an Error Boundary
(16:53) A Better Error Boundary
(23:13) Reviewing the Refactor
(24:19) Closing Notes
📺 Suggested Videos:
🔗 SWR 2.0: • This Could Be the BEST...
🔗 Skeleton Loading Components: • Skeleton Loading Compo...
📚 Tutorial References:
🔗 SWR with Suspense: swr.vercel.app/docs/suspense
🔗 react-error-boundary: www.npmjs.com/package/react-e...
🔗 React Official Website: reactjs.org/
🔗 Vite Official Website: vitejs.dev/
🔗 JSON Server: www.npmjs.com/package/json-se...
Was this React Suspense & React Error Boundary with SWR tutorial helpful? If so, please share. Let me know your thoughts in the comments.
#react #suspense #explained

Пікірлер: 59

  • @stevereid636
    @stevereid636 Жыл бұрын

    Fantastic! I always get some valuable knowledge from everyone of your videos 👍🏾

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    Glad to hear it!

  • @mocastello9253
    @mocastello92535 ай бұрын

    what a really great video and best explaination of react error boundary I've ever seen!! I've got a valuable knowledge after that and knowing more about using the APIs of that library thank you for sharing your expertise with us! all best from Berlin

  • @MOJICA7257
    @MOJICA7257 Жыл бұрын

    Good Job! Thanks For Sharing Dave.

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    You're welcome! 💯

  • @MegaClockworkDoc
    @MegaClockworkDoc Жыл бұрын

    Thank you for putting this together. 👍

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    Glad you enjoyed it!

  • @carlosraul6578
    @carlosraul65786 ай бұрын

    Fantástico, muchas gracias amigo, justo lo que ocupo

  • @TravinskiyVladislav
    @TravinskiyVladislav Жыл бұрын

    Thank you, Dave

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    You're welcome!

  • @mattburgess5697
    @mattburgess5697 Жыл бұрын

    Some of this feels like it’s being done at the wrong level. There might be an error on Dave, but not on Susan, and logically it should be able to show all the others, with an error on only the broken one. The same applies to the data access. In actual fact there were two requests, the first for the big list and one for the details. Logically the skeleton should be on the individual items rather than the whole block. I can see there being a place for placing a suspense or error boundary on the components in a loop but I do worry about the implications. In any case this is not the first time I’ve run into your content and you’ve definitely earned a sub from me. Good content aimed at real devs.

  • @okbaalla8451
    @okbaalla8451 Жыл бұрын

    Dave, you're a legend, P.E.R.F.E.C.T ❤❤❤❤

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    You are too kind - thank you!

  • @nicolaichristensen6531
    @nicolaichristensen6531 Жыл бұрын

    Apart from making super components by not following JSX standards, {component} vs , great video.

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    Thank you - timestamp and reference for me to review please 😃

  • @qwertyuio53

    @qwertyuio53

    Жыл бұрын

    ​@@DaveGrayTeachesCode Around 9:02. { content } could be turned into if you just defined it as a function that returns JSX, instead of `let content = Not like this`.

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    @@qwertyuio53 that's a very common pattern though. But yes, it could work as a function. Any references I should read on why it is not recommended?

  • @qwertyuio53

    @qwertyuio53

    Жыл бұрын

    @Dave Gray Not that I know of. I was just trying to point out the timestamp since op hasn't replied yet. Personally though, I would do it the function form so it's easier to track which are state/computed and which are components.

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    @@qwertyuio53 coming back to this as I've searched "super component" and from my findings, the pattern I'm using doesn't reach the definition I'm finding. Still curious about OP's reference here. That said, here's an example of the pattern I'm using shown in the RTK Query docs tutorial (PostsList): redux.js.org/tutorials/essentials/part-7-rtk-query-basics#displaying-posts-with-queries

  • @sarahibrahim8943
    @sarahibrahim8943 Жыл бұрын

    Thank youu 💖💖💖💖

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    Welcome!

  • @DrewSpencer
    @DrewSpencer Жыл бұрын

    Great video! Is suspense ok to use with react-query or is it the same deal as SSR?

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    Here you go: tanstack.com/query/v4/docs/react/guides/suspense

  • @sakirhusain7276
    @sakirhusain7276 Жыл бұрын

    Thanks

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    Welcome

  • @dwayneneckles
    @dwayneneckles Жыл бұрын

    What made you recommend the ZTM courses, I clicked on them expecting to see you as the teacher...Great video by the way

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    That would be nice, but no, I am not an instructor there. However, I do think they are a good resource for further learning.

  • @siddiqahmed3274
    @siddiqahmed3274 Жыл бұрын

    Sir i want to ask on what should i learn react query or useSwr? i think they both do similar things as i have watched your videos on both. And thank you for the tutorial. suspense and error boundary is really great.

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    Thank you! Good question and yes, Tanstack Query (aka React Query) is good to know. It has different features that could be useful, too. I do really like SWR 2.0 as well. Keep learning something new every day! 💯🚀

  • @bekirs4240

    @bekirs4240

    Жыл бұрын

    No wrong answer here, whatever you pick is a great option

  • @okonkwo.ify18

    @okonkwo.ify18

    Жыл бұрын

    SWR is cooler

  • @mattburgess5697

    @mattburgess5697

    Жыл бұрын

    I’ve only used React-Query, but they both look to be doing more or less the same thing. You won’t go wrong either way IMO.

  • @anazi
    @anazi Жыл бұрын

    Wow ! Thats very similar to React Query... I'll keep watching

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    Agreed! I'm enjoying SWR 2.0

  • @7doors847
    @7doors847 Жыл бұрын

    🔥! 🤘

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    💯🚀🚀

  • @ProMinecraftSprite
    @ProMinecraftSprite Жыл бұрын

    Do you have a video in which you explain the useContext hook?

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    It is a chapter in my React for Beginners series here: kzread.info/dash/bejne/oJuK2Kael9anh5s.html I also use the useContext hook in several other videos on my channel 😀

  • @madhavnagpal1860
    @madhavnagpal1860 Жыл бұрын

    in PostList we havent returned null, while loading, then how useSwr notifiy Suspense that children are in loading state ?

  • @genesissaquibal3252
    @genesissaquibal32528 ай бұрын

    when im trying to import this useErrorHandler from react-error-boundary i got an error saying that it does not provide a named export for it.

  • @vinayakmane6414
    @vinayakmane6414 Жыл бұрын

    make a full course on NestJs please :)

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    Great request!

  • @vinayakmane6414

    @vinayakmane6414

    Жыл бұрын

    @@DaveGrayTeachesCode Please fulfill it sir.

  • @folarichards8704
    @folarichards8704 Жыл бұрын

    Does anyone know why I don't get syntax highlighting when I open a vite project in VSCode?

  • @belkocik
    @belkocik Жыл бұрын

    How to implement scaling error boundary for different components (different messages depend on which page you are) in whole React application?

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    A couple of things to notice about the error boundaries that I point out in the tutorial: 1) It applies to all child components of the parent you wrap in the error boundary. No need to wrap each component. Instead think about starting at the top of the section. 2) If you use the component from the docs like I did here, it will display the actual error message which could be helpful - not a custom message. If you really wanted custom messages, you could tweak the error fallback component to provide them.

  • @alexanderten9540
    @alexanderten9540 Жыл бұрын

    Suspend detecting indirect speech in the posts of 'current' user

  • @slahomar1497
    @slahomar1497 Жыл бұрын

    can I use suspense with Redux RTK ?

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    I'm not seeing much on suspense in the RTK docs.

  • @marouaniAymen
    @marouaniAymen Жыл бұрын

    Thanks for the video, but what if, our suspense have to show a full page spinner that is transparent above the current parent component instead of returning another component ?

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    For this need, it sounds like you would need to apply a CSS class to the current component instead of changing components.

  • @marouaniAymen

    @marouaniAymen

    Жыл бұрын

    @@DaveGrayTeachesCode Ok, thanks

  • @kalupatrick6863
    @kalupatrick6863 Жыл бұрын

    Thanks. But, whats the difference between SWR and React Query

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    This question might make for a good comparison video. I just published a video on SWR 2.0 last week.

  • @ermias_bahru
    @ermias_bahru Жыл бұрын

    first comment

  • @divyanshusinghthakur1447
    @divyanshusinghthakur1447 Жыл бұрын

    Thanks

  • @DaveGrayTeachesCode

    @DaveGrayTeachesCode

    Жыл бұрын

    Welcome