【发布时间】:2020-07-20 22:09:15
【问题描述】:
我正在显示一个视图 Login.js,在该视图中单击按钮,我需要呈现模态,我已将其分离并写入另一个名为 Country.js 的文件中。
在 Login.js 文件上,我已经导入了 Country.js,然后单击按钮,我正在这样做:
show_modal = () => {
<Countries/>
}
但是什么都没有发生。我是刚开始 React Native 的菜鸟,请帮助我。
Countries.js 代码:
import React, { Component, useState } from "react";
import {
Alert,
Modal,
Text,
TouchableHighlight,
View
} from "react-native";
const Countries = () => {
console.log('called');
const [modalVisible, setModalVisible] = useState(true);
return (
<View style={styles.centeredView}>
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={() => {
Alert.alert("Modal has been closed.");
}}
>
<View style={styles.centeredView}>
<View style={styles.modalView}>
<Text style={styles.modalText}>Hello World!</Text>
<TouchableHighlight
style={{ ...styles.openButton, backgroundColor: "#2196F3" }}
onPress={() => {
setModalVisible(!modalVisible);
}}
>
<Text style={styles.textStyle}>Hide Modal</Text>
</TouchableHighlight>
</View>
</View>
</Modal>
<TouchableHighlight
style={styles.openButton}
onPress={() => {
setModalVisible(true);
}}
>
<Text style={styles.textStyle}>Show Modal</Text>
</TouchableHighlight>
</View>)
};
export default Countries;
【问题讨论】:
-
嗨,您能否添加 Country.js 代码,以便我们了解它为什么不起作用,但乍一看,我的猜测是因为您在 Country 组件中没有任何道具,所以它不知道该怎么做。跨度>
-
一些我在这里没有提到的导入。
-
您可以为 modalVisible=true 传递道具并使用模态组件,
-
我在哪里使用模态组件?
-
你看我已经默认设置了模态的状态。瓦利德·纳西尔
标签: reactjs react-native jsx