react-native-android - 在 Flatlist 项目上实现 onPress
问题描述
我试图在单击并设置为另一个类时发送平面列表项的数据。ontouch 正在工作,但我在图像中出现以下错误。另外如何将api的数据发送到另一个类并从另一个类获取?我已经实现如下:
export default class FlatSpeakers extends Component {
constructor(props) {
super(props);
this.state = { isLoading: true, data: [],selectedItem: null, }
const { navigate } = this.props.navigation;
}
onPressItem = () => {
navigate('SpeakersClick')
};
componentDidMount() {
axios.get('https://rallycoding.herokuapp.com/api/music_albums')
.then(res => {
this.setState({
isLoading: false,
data: res.data,
})
})
}
renderItem({ item }) {
return (
<TouchableOpacity onPress={()=>this.onPressItem(item)} >
<Card>
<CardSection>
<View style={styles.thumbnailContainerStyle}>
<Image
style={styles.thumbnailStyle}
source={{ uri: item.image }}
/>
</View>
<View style={styles.headerContentStyle}>
<Text style={styles.headerTextStyle}>{item.title}</Text>
<Text>{item.artist}</Text>
</View>
</CardSection>
</Card>
</TouchableOpacity>
)
}
render() {
if (this.state.isLoading) {
return (
<View style={{ flex: 1, padding: 20 }}>
<ActivityIndicator />
</View>
)
}
return (
<View style={styles.container}>
<FlatList
data={this.state.data}
renderItem={this.renderItem}
keyExtractor={(item, index) => index}
onPress={this.onPressItem}
/>
</View>
);
}
}
解决方案
this.onPressItem = this.onPressItem.bind(this)
试着穿上constructor(props)
推荐阅读
- javascript - 如何在 JavaScript 中合并两个数组的数组元素?
- sql - 带有 IF 语句的 SQL 中的动态消息
- javascript - 如何设置输入限制?
- c# - Unity3d - 更改天空盒颜色时反射不会更新
- perforce - perforce:如何检查最近(半年期间)修改的文件以及修改这些文件的人
- haskell - 如何返回在 Haskell 编程中交替组合两个函数列表产生的函数?
- android - 我可以在执行航点任务时向任何方向旋转飞机偏航吗?
- python - Django中多对多字段的PATCH
- ios - 使用 Socket 从浏览器读取请求后如何发送响应?(我正在使用 SwiftSocket)
- python - 我怎样才能让这段代码不仅仅包含循环中的第一次出现?