Skip to content

Commit ebfd947

Browse files
authored
support ErrorBoundary in asBaseComponent (#1041)
* support ErrorBoundary in asBaseComponent * fix public changes
1 parent 9b5e7ab commit ebfd947

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import React from 'react';
22
export default class UIComponent<P = {}, S = {}, SS = any> extends React.PureComponent<P, S, SS> {
3+
static defaultProps: any;
34
}

src/commons/UIComponent.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ import React from 'react';
22

33
// For use of applying dynamic context over all components
44

5-
export default class UIComponent<P = {}, S = {}, SS = any> extends React.PureComponent<P, S, SS> {}
5+
export default class UIComponent<P = {}, S = {}, SS = any> extends React.PureComponent<P, S, SS> {
6+
static defaultProps: any;
7+
}

src/commons/asBaseComponent.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,30 @@ function asBaseComponent<PROPS, STATICS = {}>(WrappedComponent: React.ComponentT
2525
static propTypes: any;
2626
static defaultProps: any;
2727

28+
state = {
29+
error: false
30+
};
31+
2832
static getThemeProps = (props: any, context: any) => {
2933
return Modifiers.getThemeProps.call(WrappedComponent, props, context);
3034
}
3135

36+
static getDerivedStateFromError(error: any) {
37+
UIComponent.defaultProps?.onError(error, WrappedComponent.defaultProps);
38+
return {error: true};
39+
}
40+
3241
render() {
3342
const themeProps = BaseComponent.getThemeProps(this.props, this.context);
3443
const modifiers = Modifiers.generateModifiersStyle(undefined, themeProps);
3544
// TODO: omit original modifiers props (left, right, flex, etc..)
3645
// Because they throws an error when being passed to RNView on Android
3746
const {forwardedRef, ...others} = themeProps;
38-
return <WrappedComponent {...others} modifiers={modifiers} ref={forwardedRef}/>;
47+
return (
48+
(this.state.error && UIComponent.defaultProps?.renderError) || (
49+
<WrappedComponent {...others} modifiers={modifiers} ref={forwardedRef} />
50+
)
51+
)
3952
}
4053
}
4154

0 commit comments

Comments
 (0)