Last updated:
2 min read
What Is and How to use  react  React Error Boundary
Gathsara Umesh
Author
0%
SHARE

What Is and How to use react React Error Boundary

What is React Error Boundary

According to react documentation...Error boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed.

If I am describing this from my experience... Everyone knows that when creating a React app, it is a fact that Errors / Bugs come when the app is built and put into production. In case of such an error, the entire React app will crash. Imagine that when such errors/bugs appear in an app that has been put into production, it is a big inconvenience to users and our app fails. If we want to show a fallback UI without crashing our app in case of such an error, we can use the Error Boundary

Let's create react application and crash it.😁 raycast-untitled.png When the counter is equal to 6 our app will crash like the below screen. Animation.gif

You can see our application went to a blank page and crashed. This can't happen when our app is at the production level.

So let's fix this issue using React Error Boundary 🤭🫡

We can create our Error Boundary code like this. raycast-untitled (1).png after creating this code we need to wrap this component using others like this. raycast-untitled (2).png

That's all. Now let's see what happens to our code with an error boundary.🤭 Animation2.gif

You see... The blank page is gone and our app is not crashing anymore. We can use an error boundary like this and we can avoid our app crashing or showing blank pages to our users.


LEAVE A COMMENT OR START A DISCUSSION

MORE ARTICLES

What is a worker thread in Node Js and How does it work?

    4 min read

What is a worker thread in Node Js and How does it work?

Node Js Worker threads Nest Js

Subscribe to Newsletter

Weekly


  • Never miss an update.
  • Get articles and snippets directly to your inbox.
  • Subscribe to stay connected and avoid getting lost among millions of websites.

Monthly


  • Coming soon...