How to overlay view on another view in React Native

Issue #254

Original post https://stackoverflow.com/a/54108708/1418457


Make our own convenient OverlayContainer. The trick is to use absolute with 100% size

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// @flow

import React from 'react'
import { View, StyleSheet } from 'react-native'

type Props = {
behind: React.Component,
front: React.Component,
under: React.Component
}

// Show something on top of other
export default class OverlayContainer extends React.Component<Props> {
render() {
const { behind, front, under } = this.props

return (
<View style={styles.container}>
<View style={styles.center}>
<View style={styles.behind}>
{behind}
</View>
{front}
</View>
{under}
</View>
)
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
height: 100,
justifyContent: 'center',
},
center: {
width: '100%',
height: '100%',
alignItems: 'center',
justifyContent: 'center',
},
behind: {
alignItems: 'center',
justifyContent: 'center',
position: 'absolute',
left: 0,
top: 0,
width: '100%',
height: '100%'
}
})

Comments