我的代码是这样的:
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, View, TouchableHighlight, Text } from 'react-native';
export default class LearnRN extends Component {
constructor(props){
super(props);
this.state = {
text:'',
}
this.press = this.press.bind(this);
}
press(){
this.setState({text:'hello'});
}
render(){
return (
<View style={styles.container}>
<TouchableHighlight onHideUnderlay={this.press}>
<View style={styles.button}/>
</TouchableHighlight>
<Text>{this.state.text}</Text>
</View>
);
}
}
补充:onShowUnderlay也无法触发,但是onPress没有问题,这是为什么?
应该用箭头函数
<TouchableHighlight onHideUnderlay={this.press}>
写为 <TouchableHighlight onHideUnderlay={() => this.press()}