react-native - 如何在本机反应中处理多个onPress
问题描述
我有一个模态组件,我有两个按钮和一个 imageView。
const ImageModal = ({ modalOpen, onClose, returnButton, customerImage, nextPage, nextPageText }) => {
const { container, modalToggle, modalClose, imageStyle } = styles;
return (
<Modal visible={modalOpen} animationType='slide'>
<View>
<View style={styles.ButtonsContainer}>
{/* Button number 1*/}
<TouchableOpacity onPress={onClose}>
<View style={styles.buttonView}>
<Text style={styles.textButton}>{returnButton}</Text>
</View>
</TouchableOpacity>
{/* Button number 2*/}
<TouchableOpacity onPress={nextPage}>
<View style={styles.buttonView}>
<Text style={styles.textButton}>{nextPageText}</Text>
</View>
</TouchableOpacity>
</View>
<Image style={styles.imageStyle} source={customerImage} />
</View>
</Modal>
我已将此组件导入我的屏幕组件。
<Modal visible={modalOpen} animationType='slide'>
<ImageModal
returnButton="Ta om"
onClose={()=> setModalOpen(false)}
customerImage={{uri: image}}
nextPage={() => navigation.navigate('SaveCase', { image })}
nextPageText="Välj"
/>
</Modal>
但是当运行应用程序时,我只能按一个按钮(onClose)。但有时该按钮适用于“onClose”和“nextPage”道具。
解决方案
从您的代码中很难确定问题出在哪里。
可能的原因可能是:
- 您的按钮重叠(最常见),
- 您错误地使用了道具。
推荐阅读
- r - 在R中使用dplyr查找日期范围内的最大值
- typescript - Typescript - 从模块导入的任何类型
- google-apps-script - 用于导出 CSV 文件的 Google 表格脚本错误
- python - 具有阈值或边界的区域增长算法
- dockerfile - 如何定义一个环境变量来保存 Dockerfile 中的当前时间
- php - 如何将 PHP 5.328 升级到 PHP 7.4.9 以使用 Oracle 11g?
- node.js - 在 NodeJS REST api 中动态获取 JSON 数据
- css - Adsense 广告有时不居中和/或出现在奇怪的地方
- git - 在 git pre-commit 中等待交互式 powershell 脚本
- windows - DPAPI 如何保护系统帐户的 MasterKey?