Skip to content

support ErrorBoundary in asBaseComponent #1041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions generatedTypes/commons/UIComponent.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
export default class UIComponent<P = {}, S = {}, SS = any> extends React.PureComponent<P, S, SS> {
static defaultProps: any;
}
4 changes: 3 additions & 1 deletion src/commons/UIComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ import React from 'react';

// For use of applying dynamic context over all components

export default class UIComponent<P = {}, S = {}, SS = any> extends React.PureComponent<P, S, SS> {}
export default class UIComponent<P = {}, S = {}, SS = any> extends React.PureComponent<P, S, SS> {
static defaultProps: any;
}
15 changes: 14 additions & 1 deletion src/commons/asBaseComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,30 @@ function asBaseComponent<PROPS, STATICS = {}>(WrappedComponent: React.ComponentT
static propTypes: any;
static defaultProps: any;

state = {
error: false
};

static getThemeProps = (props: any, context: any) => {
return Modifiers.getThemeProps.call(WrappedComponent, props, context);
}

static getDerivedStateFromError(error: any) {
UIComponent.defaultProps?.onError(error, WrappedComponent.defaultProps);
return {error: true};
}

render() {
const themeProps = BaseComponent.getThemeProps(this.props, this.context);
const modifiers = Modifiers.generateModifiersStyle(undefined, themeProps);
// TODO: omit original modifiers props (left, right, flex, etc..)
// Because they throws an error when being passed to RNView on Android
const {forwardedRef, ...others} = themeProps;
return <WrappedComponent {...others} modifiers={modifiers} ref={forwardedRef}/>;
return (
(this.state.error && UIComponent.defaultProps?.renderError) || (
<WrappedComponent {...others} modifiers={modifiers} ref={forwardedRef} />
)
)
}
}

Expand Down