为什么 onpress 函数在 React Native 中不起作用
Why onpress function not working in react native
class 文件
export class salon extends Component {
constructor(props) {
super(props);
this.state = {
status : true,
};
}
_toggleModal(){
Alert.alert('hello');
}
}
我在侧边栏中使用导航选项
我在touchable opacity中添加了onpress功能,它只对touch不起作用
<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>
查看 documentation 了解更多信息。
尝试像这样调用没有箭头函数的函数:onPress={this._toggleModal
}
你可以像这样使用箭头函数onPress={() => {this._toggleModal()}}
class 文件
export class salon extends Component {
constructor(props) {
super(props);
this.state = {
status : true,
};
}
_toggleModal(){
Alert.alert('hello');
}
}
我在侧边栏中使用导航选项
我在touchable opacity中添加了onpress功能,它只对touch不起作用
<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>
查看 documentation 了解更多信息。
尝试像这样调用没有箭头函数的函数:onPress={this._toggleModal
}
你可以像这样使用箭头函数onPress={() => {this._toggleModal()}}