Skip to content

Calc modifiers in render methods so we can access context #1035

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 1 commit into from
Nov 16, 2020
Merged
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
14 changes: 2 additions & 12 deletions src/commons/asBaseComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,21 @@ type ThemeComponent = {

function asBaseComponent<PROPS, STATICS = {}>(WrappedComponent: React.ComponentType<any>): React.ComponentClass<PROPS & ThemeComponent> & STATICS {
class BaseComponent extends UIComponent {
state = Modifiers.generateModifiersStyle(undefined, BaseComponent.getThemeProps(this.props, this.context));
static displayName: string | undefined;
static propTypes: any;
static defaultProps: any;

static getDerivedStateFromProps(nextProps: any, prevState: any) {
const themeProps = BaseComponent.getThemeProps(nextProps, undefined);
const newModifiers = Modifiers.generateModifiersStyle(undefined, themeProps);
if (!_.isEqual(newModifiers, prevState)) {
return newModifiers;
}

return null;
}

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

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 /* {...this.props} */ {...others} modifiers={this.state} ref={forwardedRef}/>;
return <WrappedComponent {...others} modifiers={modifiers} ref={forwardedRef}/>;
}
}

Expand Down