Last active
June 8, 2023 14:54
-
-
Save jaysoo/cbb81a07cc22015a72e9 to your computer and use it in GitHub Desktop.
How to hide a component in react-native
This file contains hidden or 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
/* | |
* UPDATE: Looked at the blame, turns out the negative bottom is actually for ensuring layout doesn't change during transitions. | |
* Still don't know how that works completely, but it has nothing to do with hiding (top: window.height pushes view out of viewport). | |
* | |
* I was just looking at Navigator implementation and noticed this line: | |
* https://github.com/facebook/react-native/blob/master/Libraries/CustomComponents/Navigator/Navigator.js#L110-L113 | |
* | |
*/ | |
import React, { | |
Component, | |
Dimensions, | |
StyleSheet, | |
Text, | |
View, | |
} from 'react-native'; | |
const window = Dimensions.get('window'); | |
const styles = StyleSheet.create({ | |
container: { | |
overflow: 'hidden', | |
top: 0, | |
left: 0, | |
right: 0, | |
bottom: 0, | |
position: 'absolute', | |
alignItems: 'center', | |
justifyContent:'center' | |
}, | |
// This pushes the view out of the viewport, but why the negative bottom? | |
hiddenContainer: { | |
top: window.height, | |
bottom: -window.height | |
} | |
}); | |
class Example extends Component { | |
render() { | |
return ( | |
<View style={{ flex: 1 }}> | |
<View style={[styles.container, { backgroundColor: 'red' }]}> | |
<Text>Can see me!</Text> | |
</View> | |
<View style={[styles.container, { backgroundColor: 'yellow' }, styles.hiddenContainer]}> | |
<Text>Cannot see me</Text> | |
</View> | |
<View style={[styles.container, { backgroundColor: 'blue' }, styles.hiddenContainer]}> | |
<Text>Cannot see me</Text> | |
</View> | |
</View> | |
); | |
} | |
} | |
export default Example; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment