我有一个listView通过JSON显示数据。我介绍了一个功能,并在单击每一行时,显示一个警报,包括{shop_name}。但是在第一步,它没有识别该功能。功能在同一类当我将警报放在OnPress中时,它起作用
class SearchPage extends Component {
constructor(props) {
super(props);
var dataSource = new ListView.DataSource({rowHasChanged:(r1,r2) => r1.ruid != r2.guid});
this.state = {
id: 'SearchPage',
shopid: this.props.shopid,
homesearchinpt: this.props.homesearchinpt,
dataSource: dataSource.cloneWithRows(shopsArray),
isLoading: true
};
}
componentDidMount(){
this.fetchData();
}
onSubmitPressed(){
Alert.alert(
'Alert Title',
'alertMessage',
)
}
fetchData() {
fetch(REQUEST_URL)
.then((response) => response.json())
.then((responseData) => {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(responseData),
loaded: true,
});
})
.done();
}
render() {
if (!this.state.loaded) {
return this.renderLoadingView();
}
return (
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderShops}
style={styles.listView}
/>
);
}
renderLoadingView() {
return (
<View style={styles.container}>
<Text>Loading</Text>
</View>
);
}
renderShops(shop) {
return (
<View style={styles.container} >
<TouchableOpacity onPress={this.onSubmitPressed.bind(this)}>
<Image
source={{uri: shop.shop_logo}}
style={{width: 50, height: 50}}>
</Image>
<View>
<Text style={styles.shop_name}>{shop.shop_name}</Text>
<Text style={styles.shop_description}>{shop.shop_description}</Text>
</View>
</TouchableOpacity>
</View>
);
}
};
错误:
未定义不是一个对象(评估'this.onsubmitpressed.bind')
onSubmitPressed(shopName) {
Alert.alert(
'Alert Title',
shopName,
)
}
<TouchableOpacity onPress={() => this.onSubmitPressed(shop.shop_name)}>
我重写了代码,解决了问题。谢谢