![]() |
VOOZH | about |
componentDidCatch() is a React lifecycle method used to handle errors in child components and prevent the entire app from crashing. It also allows side-effects like logging errors during the commit phase.
Syntax:
componentDidCatch(error, info)Parameters: It accepts two parameters i.e, error, and info as described below:
Follow the steps to create React application:
Step 1: Create a React application using the following command:
npx create-react-app foldernameStep 2: After creating your project folder i.e. folder name, move to it using the following command:
cd foldernameProject Structure: It will look like the following.
Example: Program to demonstrate the use of componentDidCatch() method.
Filename: App.js:
Step to Run Application: Run the application using the following command from the root directory of the project:
npm startOutput:
Reference: https://legacy.reactjs.org/docs/react-component.html#componentdidcatch