The getDerivedStateFromError() method is invoked if some error occurs during the rendering phase of any lifecycle methods or any children components. This method is used to implement the Error Boundaries for the React application. It is called during the render phase, so side-effects are not permitted in this method. For side-effects, use componentDidCatch() instead.
Parameters: It accepts the error that was thrown as a parameter.
Creating React Application:
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. folder name, move to it using the following command:
Example: Program to demonstrate the use of getDerivedStateFromError() method.
Project Structure: It will look like the following.
Step to Run Application: Run the application using the following command from the root directory of the project: