Thanks for the post, Johnson. Your post is a good based on the official doc~
One thing I'd like to mention is that, when an error boundary catches and error, it normally shows a "fallback UI".
class ErrorBoundary extends Component { // ... rest removed for brevity render() { if (this.state.hasError) { // 👇 here shows the fallback UI return <h1>Something went wrong.</h1>; } return this.props.children; } }
But once it shows the fallback, there isn't a documentation on how to "clear" the error boundary (if it's a non-critical error as an example).
I asked for a feature for react-error-boundary (by Brian Vaughn of React core team) to enable "clearing the boundary".
It's basically a bit more mature version of what's in the documentation.
And his response was to add a "key" to the Error Boundary component and change the key to re-render the children.
github.com/bvaughn/react-error-bou...
class App extends React.Component { state = { errorBoundaryKey: 0 }; handleRerenderButtonClick = () => this.forceUpdate(); handleResetButtonClick = () => this.setState(prevState => ({ errorBoundaryKey: prevState.errorBoundaryKey + 1 })); render() { return ( <div className="App"> <button onClick={this.handleRerenderButtonClick}>re-render</button> <button onClick={this.handleResetButtonClick}> reset error boundary </button> {/* ... 👇 ... */} <ErrorBoundary key={this.state.errorBoundaryKey}> <ComponentThatMayError /> </ErrorBoundary> </div> ); } }
wow, makes sense, Thanks for sharing this Sung
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Thanks for the post, Johnson. Your post is a good based on the official doc~
One thing I'd like to mention is that, when an error boundary catches and error, it normally shows a "fallback UI".
But once it shows the fallback, there isn't a documentation on how to "clear" the error boundary (if it's a non-critical error as an example).
I asked for a feature for react-error-boundary (by Brian Vaughn of React core team) to enable "clearing the boundary".
It's basically a bit more mature version of what's in the documentation.
And his response was to add a "key" to the Error Boundary component and change the key to re-render the children.
github.com/bvaughn/react-error-bou...
wow, makes sense, Thanks for sharing this Sung