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
Fantastic! I always get some valuable knowledge from everyone of your videos 👍🏾
@DaveGrayTeachesCode
Жыл бұрын
Glad to hear it!
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
Good Job! Thanks For Sharing Dave.
@DaveGrayTeachesCode
Жыл бұрын
You're welcome! 💯
Thank you for putting this together. 👍
@DaveGrayTeachesCode
Жыл бұрын
Glad you enjoyed it!
Fantástico, muchas gracias amigo, justo lo que ocupo
Thank you, Dave
@DaveGrayTeachesCode
Жыл бұрын
You're welcome!
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.
Dave, you're a legend, P.E.R.F.E.C.T ❤❤❤❤
@DaveGrayTeachesCode
Жыл бұрын
You are too kind - thank you!
Apart from making super components by not following JSX standards, {component} vs , great video.
@DaveGrayTeachesCode
Жыл бұрын
Thank you - timestamp and reference for me to review please 😃
@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
Жыл бұрын
@@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
Жыл бұрын
@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
Жыл бұрын
@@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
Thank youu 💖💖💖💖
@DaveGrayTeachesCode
Жыл бұрын
Welcome!
Great video! Is suspense ok to use with react-query or is it the same deal as SSR?
@DaveGrayTeachesCode
Жыл бұрын
Here you go: tanstack.com/query/v4/docs/react/guides/suspense
Thanks
@DaveGrayTeachesCode
Жыл бұрын
Welcome
What made you recommend the ZTM courses, I clicked on them expecting to see you as the teacher...Great video by the way
@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.
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
Жыл бұрын
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
Жыл бұрын
No wrong answer here, whatever you pick is a great option
@okonkwo.ify18
Жыл бұрын
SWR is cooler
@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.
Wow ! Thats very similar to React Query... I'll keep watching
@DaveGrayTeachesCode
Жыл бұрын
Agreed! I'm enjoying SWR 2.0
🔥! 🤘
@DaveGrayTeachesCode
Жыл бұрын
💯🚀🚀
Do you have a video in which you explain the useContext hook?
@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 😀
in PostList we havent returned null, while loading, then how useSwr notifiy Suspense that children are in loading state ?
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.
make a full course on NestJs please :)
@DaveGrayTeachesCode
Жыл бұрын
Great request!
@vinayakmane6414
Жыл бұрын
@@DaveGrayTeachesCode Please fulfill it sir.
Does anyone know why I don't get syntax highlighting when I open a vite project in VSCode?
How to implement scaling error boundary for different components (different messages depend on which page you are) in whole React application?
@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.
Suspend detecting indirect speech in the posts of 'current' user
can I use suspense with Redux RTK ?
@DaveGrayTeachesCode
Жыл бұрын
I'm not seeing much on suspense in the RTK docs.
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
Жыл бұрын
For this need, it sounds like you would need to apply a CSS class to the current component instead of changing components.
@marouaniAymen
Жыл бұрын
@@DaveGrayTeachesCode Ok, thanks
Thanks. But, whats the difference between SWR and React Query
@DaveGrayTeachesCode
Жыл бұрын
This question might make for a good comparison video. I just published a video on SWR 2.0 last week.
first comment
Thanks
@DaveGrayTeachesCode
Жыл бұрын
Welcome