我正在开发我的第一个React Native应用程序,我正在尝试将id设置为一个组件,以便在按下它并根据组件的id显示数据时可以确定此视图
这是我到目前为止所尝试的:
renderRow(rowData, sectionID, rowID){
var past_matches_circles =[];
for(var i=0; i<isPast; i++){
past_matches_circles.push(
<TouchableOpacity id={i} key = {i} collapsable={false} style={styles.small_circle} onPress={()=>this.pressOnCircle(rowData[`${i}`].MatchID)}>
</TouchableOpacity>
);
}
return (
<View>
<View style={{flexDirection:'row'}}>
{past_matches_circles}
</View>
<View style={{flexDirection:'row'}}>
<Image style={{width:100, height:100, margin:5}} source={{uri:'http://facebook.github.io/react/img/logo_og.png'}}/>
<View style={{justifyContent:'center', flex:1}}>
<Text> {rowData[0].MatchDate} </Text>
<Text> {rowData[0].HomeTeam} VS {rowData[0].AwayTeam} </Text>
</View>
</View>
</View>
);
}
pressOnCircle(i){
ToastAndroid.show('key ' + i, ToastAndroid.SHORT);
}
Run Code Online (Sandbox Code Playgroud)
但不幸的i是,无论我按哪个视图,迭代器值总是等于循环内的最后一个值.
任何人都可以帮助我获得组件的ID吗?
我正在尝试使用此代码返回到我的react本机应用程序中的上一个视图
'use strict';
var React = require('react-native');
var {
AppRegistry,
Component,
StyleSheet,
Text,
View,
BackAndroid,
Navigator
} = React;
var HomePage = require('./HomePage');
class DetailsPage extends Component{
constructor(props){
super(props);
}
render(){
return(
<View style={styles.container}>
<Text style={styles.text}>
{this.props.description}
</Text>
</View>
)
}
}
BackAndroid.addEventListener('hardwareBackPress', function() {
this.props.navigator.pop(); // line 32
return true;
});
var styles = StyleSheet.create({
container: {
flex: 1
},
text:{
color:'#000',
textAlign:'center',
fontWeight:'bold',
flex:1,
fontSize:20
},
});
module.exports = DetailsPage;
Run Code Online (Sandbox Code Playgroud)
在调试时,我看到后面的事件被成功检测到,但它在这一行崩溃,this.props.navigator.pop()给我这个错误.
无法读取undefinedhandleException的属性'props'@ D:\ React\Kora \node_modules\react-native\Libraries\JavaScriptAppEngine\Initialization\ExceptionsMana ...:61handleError …