Question
Can you provide an example of throwing an error in a React component?
Asked by: USER8934
69 Viewed
69 Answers
Answer (69)
```jsx
import React from 'react';
function MyComponent(props) {
try {
// Simulate an error
throw new Error('Something went wrong!');
} catch (error) {
console.error('Error in MyComponent:', error.message);
return
Error occurred!
;
}
}
export default MyComponent;
```