-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModalComponent.tsx
More file actions
62 lines (54 loc) · 1.53 KB
/
Copy pathModalComponent.tsx
File metadata and controls
62 lines (54 loc) · 1.53 KB
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
54
55
56
57
58
59
60
61
62
import {forwardRef, useImperativeHandle, useState} from 'react';
import {StyleSheet, Text, View} from 'react-native';
import Modal from 'react-native-modal';
import {ExampleData} from '../../App';
// This is the ref interface that will be exposed to parent.
export interface ModalRef {
openModal: (data: ExampleData) => void;
}
const ModalComponent = forwardRef<ModalRef, {}>((props, ref) => {
const [isVisible, setIsVisible] = useState(false);
const [modalData, setModalData] = useState<ExampleData>();
// This function will be callable by the parent component.
const openModal = (data: ExampleData) => {
setModalData(data); // Use the data passed by parent.
setIsVisible(true);
};
// This function will be callable by the parent component.
const closeModal = () => {
setIsVisible(false);
};
// Expose the `openModal` and `closeModal` functions to parent component.
useImperativeHandle(ref, () => ({
openModal,
closeModal,
}));
return (
<Modal
style={styles.modal}
isVisible={isVisible}
onBackdropPress={closeModal}>
{modalData && (
<View style={styles.container}>
<Text>{modalData.message}</Text>
</View>
)}
</Modal>
);
});
const styles = StyleSheet.create({
modal: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
container: {
height: '50%',
width: '80%',
borderRadius: 16,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'white',
},
});
export default ModalComponent;