javascript - React-native flatlist 不渲染?
问题描述
我的 ReactNative FlatList 没有使用这个简单的实现进行渲染。
<FlatList style={{flex:1, backgroundColor:'red'}}
data = {this.state.users}
keyExtractor={item => item.key.toString()}
renderItem={({item}) => {
return (
<ChatUserCard key={item.uid} username={item.username} />
)
}}
/>
聊天用户卡
<View style={styles.cardStyle}>
<Text style={styles.itemStyle}>{this.props.username}</Text>
<Button style={styles.buttonStyle}
title='Chat'
onPress={this.startChat} />
</View>
解决方案
我在想发生的事情是你没有把你的东西包裹FlatList
在一个View
已经flex: 1
设置好的东西中。此外,您可能可以使用您的uid
作为您的密钥,而不是key
在您的对象数据中设置 a
演示
https://snack.expo.io/@anonymoussb/so53688423
import * as React from 'react';
import { Text, View, StyleSheet, Button, FlatList } from 'react-native';
class ChatUserCard extends React.Component {
render() {
return (
<View style={styles.cardStyle}>
<Text style={styles.itemStyle}>{this.props.username}</Text>
<Button style={styles.buttonStyle}
title='Chat'
onPress={this.startChat} />
</View>
)
}
}
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
users: [
{ key: 123, uid: 123, username: 'taco' },
{ key: 456, uid: 456, username: 'cat' }
]
}
}
render() {
return (
<View style={styles.container}>
<FlatList style={{flex:1, backgroundColor:'red'}}
data = {this.state.users}
keyExtractor={item => item.key.toString()}
renderItem={({item}) => {
return (
<ChatUserCard key={item.uid} username={item.username} />
)
}}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
},
});
推荐阅读
- pyspark - 为什么在尝试 Window 函数时 pyspark 会抛出“'Column' object is not callable”?
- angular - 带有 FontAwesome 的 Angular:无法设置路径样式
- java - c++ std::vector
转为 UTF16 编码的字符串 - payment - 打开 CashApp 付款的 URL?
- c# - 如何访问授权用户自定义标签助手
- swift - SQLite.swift 中的过滤和排序查询
- python - 如何计算给定多个父节点的后代?
- php - 使用 oauth2-microsoft 从 Microsoft Graph 检索用户详细信息时出错
- ios - 问题是关于 localhost、Swift URL、NetBeans 和 Xampp
- ios - 返回错误的 UICollectionViewCell 尺寸 - Swift