Current section: Suspense img 29 exercises
solution

Img Error Boundary

Loading solution

Transcript

00:00 All right, so we want to wrap this image in an error boundary. We could put that error boundary up where we're using the image, right up here, right here. We could put the error boundary right there, but I want this image component to be like a pretty generic image component we can use all over the place.

00:18 So we're going to have a special ship image component, which is going to be responsible for rendering the error boundary. So the specific behavior we want for the specific image instance. So with that, we're going to make another function, ship image. It's going to actually be pretty similar. We're going to take our props. That's going to be our React component props.

00:37 We're not going to do the suspense stuff, though. We are going to render a uppercase image with all those props, but we're going to render around it an error boundary. And for us, our fallback of that error boundary is going to be a regular image with those props.

00:56 And we need three dots. There we go. Syntax errors are not good. And we are done. So if there is an error, then it will fall back to a regular image, and that will allow us to render the rest of our stuff without having any trouble. So that should take care of these problems

01:16 where we blow up the entire app if it's not exactly what it's supposed to be. So this is an improvement. Error boundaries can be nested. And that way, if a certain part of your UI fails, the rest of the UI doesn't have to be replaced by the fallback. So there you go.