-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Prevent the Playground from crashing on a form error. (#3164)
Co-authored-by: Heath C <[email protected]>
- Loading branch information
1 parent
33896dc
commit 22772c7
Showing
3 changed files
with
103 additions
and
59 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from "react"; | ||
|
||
class ErrorBoundary extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { hasError: false, error: null }; | ||
} | ||
|
||
/** Update state so the next render will show the fallback UI. */ | ||
static getDerivedStateFromError(error) { | ||
return { hasError: true, error: error }; | ||
} | ||
|
||
resetErrorBoundary = () => { | ||
this.setState({ hasError: false, error: null }); | ||
} | ||
|
||
/** You can render any custom fallback UI */ | ||
render() { | ||
if (this.state.hasError) { | ||
return ( | ||
<div className="alert alert-danger"> | ||
<p>The following error was encountered:</p> | ||
<pre>{this.state.error.message}</pre> | ||
<button className="btn" onClick={this.resetErrorBoundary}> | ||
Refresh Form | ||
</button> | ||
</div> | ||
); | ||
} | ||
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