尽管接收到正确的数据,React Native FlatList renderItem 没有 return 任何东西
React Native FlatList renderItem does not return anything despite receiving correct data
这里是 renderItem
函数:
const renderItem = ({item, index}) => {
getPost(item.id)
.then((postContent) => {
if (postContent.postType === 0) {
console.log(postContent);
console.log('Rendering type text post');
return <TextPost item={postContent} index={index} />;
} else if (postContent.postType === 1) {
console.log(postContent);
console.log('Rendering type image post');
return <ImagePost item={postContent} index={index} />;
} else {
console.log(postContent);
console.log('Rendering type link post');
return <LinkPost item={postContent} index={index} />;
}
})
.catch((err) => console.log(err));
};
renderItem
处理我的数据库中存在的 post 个 ID 数组。对于每个 id,FlatList 应该使用 item.id
作为参数调用我的 getPost
方法,然后使用相应的 post id 从我的数据库中获取 post 数据。这是可行的,因为我的 console.log
语句为给定的 post id 数组打印出正确的数据。但是,return 语句不会 return 任何组件,并且 FlatList
呈现空白。
这是 FlatList 以防万一:
<FlatList data={data} renderItem={renderItem} style={styles.list} />
在渲染中,你不能使用异步函数,因为渲染不能等待你的函数被执行。而是在 componentDidMount 中创建数组并将其放入状态并在 flatlist 中使用状态。
这里是 renderItem
函数:
const renderItem = ({item, index}) => {
getPost(item.id)
.then((postContent) => {
if (postContent.postType === 0) {
console.log(postContent);
console.log('Rendering type text post');
return <TextPost item={postContent} index={index} />;
} else if (postContent.postType === 1) {
console.log(postContent);
console.log('Rendering type image post');
return <ImagePost item={postContent} index={index} />;
} else {
console.log(postContent);
console.log('Rendering type link post');
return <LinkPost item={postContent} index={index} />;
}
})
.catch((err) => console.log(err));
};
renderItem
处理我的数据库中存在的 post 个 ID 数组。对于每个 id,FlatList 应该使用 item.id
作为参数调用我的 getPost
方法,然后使用相应的 post id 从我的数据库中获取 post 数据。这是可行的,因为我的 console.log
语句为给定的 post id 数组打印出正确的数据。但是,return 语句不会 return 任何组件,并且 FlatList
呈现空白。
这是 FlatList 以防万一:
<FlatList data={data} renderItem={renderItem} style={styles.list} />
在渲染中,你不能使用异步函数,因为渲染不能等待你的函数被执行。而是在 componentDidMount 中创建数组并将其放入状态并在 flatlist 中使用状态。