react-native - 无法打印console.log in button react-native
问题描述
我为可重用性创建了一个自定义按钮组件这是我的按钮代码
export default function AppButton({ title, color = "", marginVertical = 0 }) {
return (
<TouchableHighlight
style={[
styles.button,
{ backgroundColor: colors[color], marginVertical: marginVertical }]}
>
<Text style={styles.text}>{title}</Text>
</TouchableHighlight>
);
}
这是我要在其中打印控制台 .log 的按钮
<AppButton
title="login"
onPress={() => console.log("email and password recieved")}
/>
解决方案
试试这个,你需要将 onpress 函数作为道具传递给可触摸的高亮。
export default function AppButton({ title, color = "", marginVertical = 0 }) {
return (
<TouchableHighlight
onPress={onClick}
style={[
styles.button,
{ backgroundColor: colors[color], marginVertical: marginVertical },
]}
>
<Text style={styles.text}>{title}</Text>
</TouchableHighlight>
);
}
<AppButton
title="login"
onClick={() => console.log("email and password recieved")}
/>
更多细节可以在文档中找到:https ://reactnative.dev/docs/touchablehighlight
推荐阅读
- python - 来自图表的微笑
- ffmpeg - 如何每隔 yy 秒剪切 xx 秒并使用 ffmpeg 在视频中循环直到结束
- java - 获取直到特定时间点的时间
- azure - IP 地址如何在 Azure vnet 中连接到 vnet 连接
- php - 一旦在任何事件发生后重新获取,就使用 ajax for table。?
- css - 数据源 mat-cell 高度和数据
- python - Python - 从导入 PostgreSQL 的 csv 文件中删除特殊字符
- c# - 如何使用 cookie 保护 asp.net 核心中间件?
- java - iBatis 语句外部化
- redis - redis哨兵故障转移后返回老主人