Skip to content
This repository has been archived by the owner on Feb 25, 2020. It is now read-only.

fix: fix layout when starting in landscape #192

Merged
merged 2 commits into from
Oct 21, 2019
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
22 changes: 14 additions & 8 deletions src/utils/withDimensions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ export const isOrientationLandscape = ({ width, height }: DimensionsType) =>
export default function withDimensions<Props extends InjectedProps>(
WrappedComponent: React.ComponentType<Props>
): React.ComponentType<Pick<Props, Exclude<keyof Props, keyof InjectedProps>>> {
const { width, height } = Dimensions.get('window');

class EnhancedComponent extends React.Component {
static displayName = `withDimensions(${WrappedComponent.displayName})`;

state = {
dimensions: { width, height },
isLandscape: isOrientationLandscape({ width, height }),
};
constructor(props: Props) {
super(props);

const { width, height } = Dimensions.get('window');
this.state = {
dimensions: { width, height },
isLandscape: isOrientationLandscape({ width, height }),
};
}

componentDidMount() {
Dimensions.addEventListener('change', this.handleOrientationChange);
Expand All @@ -37,8 +40,11 @@ export default function withDimensions<Props extends InjectedProps>(
}

handleOrientationChange = ({ window }: { window: ScaledSize }) => {
const isLandscape = isOrientationLandscape(window);
this.setState({ isLandscape });
const { width, height } = window;
this.setState({
dimensions: { width, height },
isLandscape: isOrientationLandscape({ width, height }),
});
};

render() {
Expand Down