Skip to content

Commit cda3400

Browse files
committed
Update error boundaries interview questions
1 parent f192279 commit cda3400

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@
11651165
11661166
*Error boundaries* are 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.
11671167
1168-
A class component becomes an error boundary if it defines a new lifecycle method called `componentDidCatch(error, info)`:
1168+
A class component becomes an error boundary if it defines a new lifecycle method called `componentDidCatch(error, info)` or `static getDerivedStateFromError() `:
11691169
11701170
```jsx harmony
11711171
class ErrorBoundary extends React.Component {
@@ -1175,12 +1175,15 @@
11751175
}
11761176

11771177
componentDidCatch(error, info) {
1178-
// Display fallback UI
1179-
this.setState({ hasError: true })
11801178
// You can also log the error to an error reporting service
11811179
logErrorToMyService(error, info)
11821180
}
11831181

1182+
static getDerivedStateFromError(error) {
1183+
// Update state so the next render will show the fallback UI.
1184+
return { hasError: true };
1185+
}
1186+
11841187
render() {
11851188
if (this.state.hasError) {
11861189
// You can render any custom fallback UI

0 commit comments

Comments
 (0)