React native React Native ListView onPress-未定义不是对象
我似乎一直在按按钮调用函数。无论我尝试什么方法,我都会得到不同的未定义错误 我正在运行react native 0.24,目前只为Android设备编写代码 请参阅下面的代码:React native React Native ListView onPress-未定义不是对象,react-native,React Native,我似乎一直在按按钮调用函数。无论我尝试什么方法,我都会得到不同的未定义错误 我正在运行react native 0.24,目前只为Android设备编写代码 请参阅下面的代码: class ListPage extends Component { constructor(props){ super(props); this.nav = props.nav; this.route = props.route; this.ds = new Li
class ListPage extends Component {
constructor(props){
super(props);
this.nav = props.nav;
this.route = props.route;
this.ds = new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
});
this.state = ({
visible: false,
dataSource: this.ds.cloneWithRows(props.fullList),
});
}
goToPage(name){
this.nav.push({ name: name });
}
loadModal() {
this.setState({ visible: true });
}
list(row){
return (
<View style={styles.box}>
<TouchableNativeFeedback underlayColor="#FFF"
onPress={this.goToPage.bind(this,'ShowRow')}>
<View>
...ShowRow Button
</View>
</TouchableNativeFeedback>
<TouchableNativeFeedback
onPress={this.loadModal.bind(this)} >
<View style={styles.button}>
...Modal Button
</View>
</TouchableNativeFeedback>
</View>
);
}
render(){
return (
<View style={styles.container}>
<Modal visible={this.state.visible}
onRequestClose={() => {this.setState({visible: false}) }} >
<View>
<Text>I am a Modal!</Text>
</View>
</Modal>
<ListView
dataSource={this.state.dataSource}
renderRow={this.list} />
</View>
);
}
}
它现在表示undefined不是this.nav和this.setState的对象
但如果我这样做:
constructor() {
...
goToPage = function(name){
props.nav.push(...);
}
}
这是可行的……但我知道这不是正确的方法,而且我也不能为模式设置状态。为什么不使用第一种方法?非常感谢。好的,找到了解决方案。在renderRow中,我需要像这样绑定函数:
<ListView dataSource={this.state.dataSource}
renderRow={this.list.bind(this)} />
现在我可以用第一个方法调用函数了
<ListView dataSource={this.state.dataSource}
renderRow={this.list.bind(this)} />