react-native - 检索产品列表并显示它们
问题描述
我正在尝试从我的 API 中检索产品列表。目前,什么都没有显示,页面当前是空的,除了我的后退按钮,我不明白为什么。我使用的两个函数都可以正常工作,因为 console.log 都可以工作。你可以帮帮我吗 ?一切看起来都很好,console.log 在终端中工作,但我无法在应用程序中显示任何内容。
我试过这个小吃:https ://snack.expo.io/O4oPj8-Qz
const Item = ({ item, onPress, style }) => (
<TouchableOpacity onPress={onPress} style={[styles.productItem, style]}>
<Text style={[styles.h4, {textAlign: "left"}]}>
{item.name}
</Text>
</TouchableOpacity>
);
export default class Products extends Component {
constructor(props) {
super(props);
this.state = {
selectedId: '',
setSelectedId: '',
listData: '',
currentPage: 1,
loadMoreVisible: true,
loadMoreVisibleAtEnd: false,
displayArray: null
}
};
initListData = async () => {
let list = await getProducts(1);
console.log(list)
if (list) {
this.setState({
displayArray: list,
loadMoreVisible: (list.length >= 15 ? true : false),
currentPage: 2
});
}
};
setNewData = async (page) => {
let list = await getProducts(parseInt(page));
if (list) {
this.setState({
displayArray: this.state.displayArray.concat(list),
loadMoreVisible: (list.length >= 15 ? true : false),
loadMoreVisibleAtEnd: false,
currentPage: parseInt(page)+1
});
}
};
loadMore() {
this.setNewData(this.state.currentPage);
}
displayBtnLoadMore() {
this.setState({
loadMoreVisibleAtEnd: true
});
}
async UNSAFE_componentWillMount() {
this.initListData();
}
render() {
return (
<View>
{this.state.displayArray !== null && this.state.displayArray.length > 0 ? (
<View style={{ flex: 1, marginBottom: 100 }}>
<SafeAreaView style={styles.container}>
<FlatList
data={this.state.displayArray}
extraData={this.selectedId}
onEndReached={() => this.displayBtnLoadMore()}
renderItem={({item})=>
<View style={{flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center'}}>
<Item
item={item}
onPress={() => this.props.navigation.navigate('ProductDetails', {productId: parseInt(item.id)})}
/>
</View>
}
keyExtractor={item => "product-" + item.id.toString()}
style={{width:"90%"}}
/>
{this.state.loadMoreVisible === true && this.state.loadMoreVisibleAtEnd === true ? (
<Button title=" + " onPress={()=>{this.loadMore()}}></Button>
) : null
}
<View style={styles.container}>
<Text>{"\n"}</Text>
<TouchableOpacity
style={styles.touchable2}
onPress={() => this.props.navigation.goBack()}
>
<View style={styles.view2}>
<Text style={styles.textimg2}>
back
</Text>
</View>
</TouchableOpacity>
</View>
<Text>{"\n\n"}</Text>
</SafeAreaView>
</View>
) : (
<View style={styles.container}>
<Text>{"\n\n" + (this.state.displayArray === null ? i18n.t("products.searching") : i18n.t("products.nodata")) + "\n\n\n"}</Text>
<TouchableOpacity
style={styles.touchable2}
onPress={() => this.props.navigation.goBack()}
>
<View style={styles.view2}>
<Text style={styles.textimg2}>
Back
</Text>
</View>
</TouchableOpacity>
</View>
)}
</View>
);
};
}
解决方案
您没有添加flex: 1
而且not calling the API in the right way
,这里是解决方案的小吃。
推荐阅读
- python - 有条件地对行进行子集,但保留所有 NA 值
- java - 获取错误文件无法解析为 Visual Studio 代码中的 java 类型
- javascript - 如何确保用户输入的数据是 JavaScript 中的特定数据类型?
- docker - 在带有 buildkit 并运行 --mount 的 docker 中,为什么 cabal install 正在下载缓存的包?
- superset - 如何将变量放入超集中?
- lua - 罗技 LUA 脚本添加睡眠定时器
- ios - iOS 公共 API CTTelephonyNetworkInfo 在漫游时返回错误的 MNC
- c - 在 C 中的宏之外使用反斜杠换行符
- go-cd - Gocd 代理在将 go 服务器升级到 21.2.0 后失去联系(代理端的 java home 出错,但它是正确的)
- python - 如何使所有结果出现