-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error Boundary
- Loading branch information
Showing
4 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React, { Component } from "react"; | ||
|
||
class ErrorBoundary extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
hasError: false, | ||
}; | ||
} | ||
|
||
static getDerivedStateFromError(error) { | ||
// Update state so the next render will show the fallback UI. | ||
return { | ||
hasError: true, | ||
}; | ||
} | ||
|
||
componentDidCatch(error, info) { | ||
// log the error to an error reporting service | ||
console.log(error); | ||
console.log(info); | ||
} | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
// render any custom fallback UI | ||
return <h2>Something went wrong</h2>; | ||
} | ||
return this.props.children; | ||
} | ||
} | ||
|
||
export default ErrorBoundary; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import React from "react"; | ||
|
||
function Hero({ heroName }) { | ||
if (heroName === "Joker") { | ||
throw new Error("Not a hero!"); | ||
} | ||
return <div>{heroName}</div>; | ||
} | ||
|
||
export default Hero; |