为什么onpress函数在本机反应中不起作用

时间:2018-11-29 07:15:40

标签: reactjs react-native react-native-navigation

课程文件

 export class salon extends Component {

    constructor(props) {
    super(props);
    this.state = {
      status : true,
      };  
    }

     _toggleModal(){
     Alert.alert('hello');
     }

}

我在侧边栏中使用了导航选项

我在可触摸的不透明度中添加了onpress功能,并且仅在触摸时不起作用

 <TouchableOpacity onPress={() =>  {this._toggleModal}}>
 </TouchableOpacity>

3 个答案:

答案 0 :(得分:1)

您应该将函数绑定到构造函数上或使用中。

在构造函数中:

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以获得更多信息。

答案 1 :(得分:0)

尝试调用不带箭头功能的函数,例如:onPress={this._toggleModal

答案 2 :(得分:0)

您可以使用像这样的箭头功能onPress={() => {this._toggleModal()}}

相关问题