[英]React Native FlatList Rendering
我是 RN 的新手,我在使用平面列表渲染 API 中的项目时遇到了一些问题。 我使用服务器的分页来为每个请求返回 10 个项目,并使用 loadMore function 来更改页面和检索新数据。 问题是当我滚动几次(4-5 页)时出现此错误,我很困惑如何解决它。 如果有人可以为我提供修复,我将非常感激。 提前致谢!
这是我的 API 获取 function =>
export const fetchProducts = (page) => {
return async (dispatch) => {
dispatch(fetchingFromServer());
try {
await fetch(SITE_URL + products + `&page=${page}` + '&per_page=12' + AUTH_PARAMS)
.then((res) => res.json())
.then((json) => dispatch(fetchingProductsSuccess(json)));
} catch (err) {
dispatch(fetchinProductsFailed(err));
}
};
};
这是我的 flatList =>
<FlatList
data={data || []}
numColumns={2}
maxToRenderPerBatch={5}
keyExtractor={(item, index) => {
return `${item.name}-${index}`;
}}
renderItem={({ item }) => {
return this._renderItem(item);
}}
onEndReached={() => this.loadMoreData()}
onEndReachedThreshold={2}
ListFooterComponent={this.renderFooter.bind(this)}
/>
这是渲染项目 function =>
_renderItem(item) {
return (
<View style={styles.containerP}>
<Image style={styles.image} source={{ uri: item.images[0].woocommerce_thumbnail }} />
<Text numberOfLines={1} style={styles.name}>
{item.name}
</Text>
{item.regular_price !== '' && item.price + '' !== item.regular_price + '' ? (
<View style={styles.prices}>
<Text style={styles.price_discounted}>{item.regular_price}лв</Text>
<Text style={styles.price}>{item.price}лв</Text>
</View>
) : (
<Text style={styles.price}>{item.price}лв</Text>
)}
</View>
);
}
并加载更多 function =>
loadMoreData() {
const { fetching_from_server, isListEnd } = this.state;
if (!fetching_from_server && !isListEnd) {
this.setState({ fetching_from_server: true }, () => {
this.props.productActions.fetchProducts(this.page);
});
}
}
您的某些商品似乎缺少图片。 尝试添加检查以确保每个项目都存在图像。 这样的事情应该有效:
_renderItem(item) {
return (
<View style={styles.containerP}>
{item.images && item.images.length ? (
<Image
style={styles.image}
source={{uri: item.images[0].woocommerce_thumbnail}}
/>
) : null}
<Text numberOfLines={1} style={styles.name}>
{item.name}
</Text>
{item.regular_price !== '' &&
item.price + '' !== item.regular_price + '' ? (
<View style={styles.prices}>
<Text style={styles.price_discounted}>{item.regular_price}лв</Text>
<Text style={styles.price}>{item.price}лв</Text>
</View>
) : (
<Text style={styles.price}>{item.price}лв</Text>
)}
</View>
);
}
为了进一步解释您看到的错误。 item.images
应该是一个对象数组,但如果没有图像,那么item.images[0]
将是未定义的。 然后,您尝试访问 object 属性woocommerce_thumbnail
但item.images[0]
未定义,因此您会收到“undefined is not an object”错误。
再见,您在_renderItem
中的item
似乎undefined
(错误说“未定义不是对象”)。 因此,您可以通过执行以下操作轻松解决:
_renderItem(item) {
return (
{item &&
<View style={styles.containerP}>
<Image style={styles.image} source={{ uri: item.images[0].woocommerce_thumbnail }} />
<Text numberOfLines={1} style={styles.name}>
{item.name}
</Text>
{item.regular_price !== '' && item.price + '' !== item.regular_price + '' ? (
<View style={styles.prices}>
<Text style={styles.price_discounted}>{item.regular_price}лв</Text>
<Text style={styles.price}>{item.price}лв</Text>
</View>
) : (
<Text style={styles.price}>{item.price}лв</Text>
)}
</View>}
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.