reactjs - 为什么 onpress 功能在本机反应中不起作用
问题描述
类文件
export class salon extends Component {
constructor(props) {
super(props);
this.state = {
status : true,
};
}
_toggleModal(){
Alert.alert('hello');
}
}
我在边栏中使用导航选项
我在可触摸的不透明度中添加了 onpress 功能,它不能仅触摸
<TouchableOpacity onPress={() => {this._toggleModal}}>
</TouchableOpacity>
解决方案
您应该在构造函数或使用中绑定您的函数。
在构造函数中:
export class salon extends Component {
constructor(props) {
super(props);
this.state = {
status : true,
};
this._toggleModal = this._toggleModal.bind(this);
}
_toggleModal(){
Alert.alert('hello');
}
}
正在使用:
<TouchableOpacity
onPress={() => {this._toggleModal.bind(this)}}>
</TouchableOpacity>
查看文档以获取更多信息。
推荐阅读
- angular - 如何在角度测试用例中配置路由器
- sql - 如果为空,如何检查多列然后按顺序使用下一列?
- python - 生成器函数总是两次产生相同的东西
- css - Safari 上涉及不透明度、缩放和 translateY 问题的 CSS 动画
- c# - 具有不符合某些条件的子节点的所有节点的 XPath?
- javascript - 如何使用 vanilla javascript 在画布中旋转矩形,使矩形的坐标也随旋转而变化?
- generator - 如何让 redux-saga 生成器“采取”行动并返回结果?
- python - 在二级函数中使用“自我”
- excel - 如何通过匹配单元格值将范围复制并粘贴到另一个工作表
- reactjs - Material-UI:更改 TextField 中的自动填充背景颜色