Your parent component should have a state like modalVisible
to handle the modal visibility.
So your parent component should pass the function prop like onPressClose
ParentComponent:
import React, {useState} from 'react';
import { View, Text } from 'react-native';
import Modal from 'react-native-modal';
function ParentComponent() {
const [visible, setVisible] = useState(true); // Starts with what visibility you want.
return (
<MyModal visible={visible} onPressClose={() => setVisible(false)} />
);
}
ModalComponent:
// imports...
function MyModal ({visible, onPressClose}){
return (
<Modal visible={visible}>
<View>
<Text> this is my Modal</Text>
</View>
<View>
<Text onPress={onPressClose}> Dismiss </Text>
</View>
</Modal>
);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…