简体   繁体   English

使用可见道具从不同组件反应本机模态打开模态

[英]React native modal open modal from different component using visible prop

How could i open Modal from different component file using my visible prop that is passed inside isVisible with some state and button.我如何使用我的可见道具从不同的组件文件打开模态,该道具通过一些 state 和按钮在 isVisible 内部传递。 Right now the modal is not opening.现在模式没有打开。 I am using react-native-modal ( https://github.com/react-native-modal/react-native-modal )我正在使用 react-native-modal ( https://github.com/react-native-modal/react-native-modal )

 type ModalProps = {
        visible: boolean;
    };

export function Modal({ visible }: ModalProps) {
    return (
        <ReactNativeModal isVisible={visible} >
                <Pressable>
                    <Icon size={14} name="icon1" />
                </Pressable>
        </ReactNativeModal>
    );
}

Calling component in different file在不同的文件中调用组件

 const [isModalVisible, setModalVisible] = React.useState(false);

    function handleModalClose() {
        setModalVisible(false);
    }

    function handleModalOpen() {
        setModalVisible(true);
    }

<Button title="Modal one" onPress={handleModalOpen}>
 <Modal visible={isModalVisible}></Modal>
</Button>

You should pass isModalVisible as visible prop to the Modal component:您应该将isModalVisible作为visible属性传递给Modal组件:

<Modal visible={isModalVisible}></Modal>

If you need to close the modal from the Modal component, you can pass also the setModalVisible function:如果您需要从Modal组件关闭模态,您也可以传递setModalVisible function:

type ModalProps = {
    visible: boolean;
    setModalVisible: React.Dispatch<React.SetStateAction<boolean>>;
};

export function Modal({ visible, setModalVisible }: ModalProps) {
    const handleClose = () => {
        setModalVisible(false);
    }

    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM