Created
March 7, 2021 22:05
-
-
Save lubaochuan/3869e5ecf0fb349151eb0cd23b18904b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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