英文:
Why do I have to click two times to open the <Modal /> when I return from a page?
问题
我创建了一个按钮类型,用于在主页上打开模态框,模态框在单击叠加层时可以正常打开和关闭,问题是当我在包含列表的页面使用<Modal />
组件,并且在返回主页后,如果我单击该按钮以打开<Modal />,则第一次单击时它不会打开。我必须点击两次才能打开它。这里问题是什么?我无法理解。
这是我的代码:
英文:
I made a button type to open the modal on the Home page, the modal is coming fine and closing fine when click on the overlay, the problem is when I got some page with the list inside<Modal />
component and when I come back to the Home page and then if I click on that button to open the <Modal /> it doesn't open on first click yet. I have to click two times to open it. What is the issue here? I can't understand.
Here is my code:
//Home.js
const modalData = [
{ key: "Add Dealer", navigateTo: "AddDealer" },
{ key: "Add Franchise", navigateTo: "AddFranchise" },
{ key: "New Lead", navigateTo: "AddNewLeads" },
];
export default function Home() {
const [modalVisible, setModalVisible] = useState(false);
const renderPopList = ({ item, index }) => {
const lastItem = index === modalData.length - 1;
return (
<TouchableOpacity onPress={() => navigation.navigate(item.navigateTo)}>
<Text style={[styles.links, lastItem && styles.lastLink]}>
{item.key}
</Text>
</TouchableOpacity>
);
};
const toggleModal = () => {
setModalVisible(!modalVisible);
};
return (
<>
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={toggleModal}
onTouchStart={toggleModal}
style={styles.modalWrapper}
>
<TouchableWithoutFeedback onPress={toggleModal}>
<View
style={{ flex: 1, backgroundColor: "rgba(0,0,0,0.1)" }}
onPress={toggleModal}
></View>
</TouchableWithoutFeedback>
<FlatList
data={modalData}
renderItem={renderPopList}
onPress={toggleModal}
style={[styles.modal]}
/>
</Modal>
//Button Here
<View style={styles.bottomBtnWrap}>
<TouchableOpacity style={styles.addBtn} onPress={toggleModal}>
<FontAwesome color="#fff" name="plus" size={30} />
</TouchableOpacity>
</View>
</>
);
}
答案1
得分: 0
你可以使用这段代码进行操作:
//Home.js
const modalData = [
{ key: "添加经销商", navigateTo: "AddDealer" },
{ key: "添加特许经营", navigateTo: "AddFranchise" },
{ key: "新线索", navigateTo: "AddNewLeads" },
];
export default function Home({ navigation }) {
const [modalVisible, setModalVisible] = useState(false);
useEffect(() => {
const unsubscribe = navigation.addListener("blur", () => {
setModalVisible(false);
});
return unsubscribe;
}, [navigation]);
const renderPopList = ({ item, index }) => {
const lastItem = index === modalData.length - 1;
return (
<TouchableOpacity onPress={() => navigation.navigate(item.navigateTo)}>
<Text style={[styles.links, lastItem && styles.lastLink]}>
{item.key}
</Text>
</TouchableOpacity>
);
};
const toggleModal = () => {
setModalVisible(!modalVisible);
};
return (
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={toggleModal}
onTouchStart={toggleModal}
style={styles.modalWrapper}
>
<TouchableWithoutFeedback onPress={toggleModal}>
<View
style={{ flex: 1, backgroundColor: "rgba(0,0,0,0.1)" }}
onPress={toggleModal}
></View>
</TouchableWithoutFeedback>
<FlatList
data={modalData}
renderItem={renderPopList}
onPress={toggleModal}
style={[styles.modal]}
/>
</Modal>
//按钮位置
<View style={styles.bottomBtnWrap}>
<TouchableOpacity style={styles.addBtn} onPress={toggleModal}>
<FontAwesome color="#fff" name="plus" size={30} />
</TouchableOpacity>
</View>
);
}
英文:
you can use this code for it
//Home.js
const modalData = [
{ key: "Add Dealer", navigateTo: "AddDealer" },
{ key: "Add Franchise", navigateTo: "AddFranchise" },
{ key: "New Lead", navigateTo: "AddNewLeads" },
];
export default function Home({ navigation }) {
const [modalVisible, setModalVisible] = useState(false);
useEffect(() => {
const unsubscribe = navigation.addListener("blur", () => {
setModalVisible(false);
});
return unsubscribe;
}, [navigation]);
const renderPopList = ({ item, index }) => {
const lastItem = index === modalData.length - 1;
return (
<TouchableOpacity onPress={() => navigation.navigate(item.navigateTo)}>
<Text style={[styles.links, lastItem && styles.lastLink]}>
{item.key}
</Text>
</TouchableOpacity>
);
};
const toggleModal = () => {
setModalVisible(!modalVisible);
};
return (
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={toggleModal}
onTouchStart={toggleModal}
style={styles.modalWrapper}
>
<TouchableWithoutFeedback onPress={toggleModal}>
<View
style={{ flex: 1, backgroundColor: "rgba(0,0,0,0.1)" }}
onPress={toggleModal}
></View>
</TouchableWithoutFeedback>
<FlatList
data={modalData}
renderItem={renderPopList}
onPress={toggleModal}
style={[styles.modal]}
/>
</Modal>
//Button Here
<View style={styles.bottomBtnWrap}>
<TouchableOpacity style={styles.addBtn} onPress={toggleModal}>
<FontAwesome color="#fff" name="plus" size={30} />
</TouchableOpacity>
</View>
);
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论