Skip to content

Instantly share code, notes, and snippets.

@lubaochuan
Created March 7, 2021 22:05
Show Gist options
  • Save lubaochuan/3869e5ecf0fb349151eb0cd23b18904b to your computer and use it in GitHub Desktop.
Save lubaochuan/3869e5ecf0fb349151eb0cd23b18904b to your computer and use it in GitHub Desktop.
import Constants from 'expo-constants';
import { Platform, StyleSheet, View } from 'react-native';
import PropTypes from 'prop-types';
import React from 'react';
export default class MeasureLayout extends React.Component {
static propTypes = {
children: PropTypes.func.isRequired,
};
state = {
layout: null,
};
handleLayout = event => {
const { nativeEvent: { layout } } = event;
this.setState({
layout: {
...layout,
y:
layout.y +
(Platform.OS === 'android' ? Constants.statusBarHeight : 0),
},
});
};
render() {
const { children } = this.props;
const { layout } = this.state;
// Measure the available space with a placeholder view set to
// flex 1
if (!layout) {
return (
<View onLayout={this.handleLayout} style={styles.container} />
);
}
return children(layout);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment