jsketcher/modules/ui/errorBoundary.js
2018-07-02 19:16:43 -07:00

30 lines
No EOL
637 B
JavaScript

import React from 'react';
export default function errorBoundary(message, fix) {
return function(Comp) {
return class extends React.Component {
state = {
hasError: false,
fixAttempt: false
};
componentDidCatch() {
this.setState({hasError: true});
if (!this.state.fixAttempt) {
if (fix) {
fix(this.props);
this.setState({hasError: false, fixAttempt: true});
}
}
}
render() {
if (this.state.hasError) {
return message || null;
}
return <Comp {...this.props} />;
}
}
}
}