gpt4 book ai didi

javascript - react 原生 : set id to a component and get this component's id

转载 作者:行者123 更新时间:2023-12-04 14:10:26 25 4
gpt4 key购买 nike

我正在开发我的第一个 React Native 应用程序,我正在尝试为组件设置一个 id,以便在按下它并根据组件的 id 显示数据时确定这个 View

这是我到目前为止所尝试的:

 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);
}

但不幸的是迭代器 i value 总是等于它在循环内的最后一个值,无论我按下哪个 View 。

谁能帮我获取组件的ID?

最佳答案

几天后,我终于解决了这个问题。我将发布它以供谁来回答这个问题。

我将 View 推送到我的数组中而不返回所以 i 的值总是一样的。

所以我做了什么:

renderCircle(id) {
return <TouchableOpacity key={id} style={styles.small_circle} onPress={() => this.pressOnCircle(id)}>
</TouchableOpacity>;
}
renderRow(rowData, sectionID, rowID){
var past_matches_circles =[];
for(var i=0; i<isPast; i++){
past_matches_circles.push(
this.renderCircle(i)
);
}
var { page, animationsAreEnabled } = this.state;
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>
);
}

希望这会有所帮助。

关于javascript - react 原生 : set id to a component and get this component's id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36553132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com