javascript - 在 React Native 中从 API 设置数据
问题描述
我从 api 获取数据,我可以在控制台中显示数据,但它没有在应用程序中显示。我尝试了很多方法,但它仍然没有在应用程序中显示。这就是我获取api的方式。
const [dataProduct,setDataProduct] = useState([]);
useEffect(() => {
getProductsApi();
},[])
const getProductsApi = async () => {
axios.get(productUrl)
.then((res) => {
// console.log(res.data)
setDataProduct(res.data)
}).catch(err => console.log(err))
// const response = await fetch(productUrl);
// const data = await response.json();
// try{
// setDataProduct(data);
// dataProduct.forEach(pro => {
// return(console.log(pro.title.rendered));
// })
// }catch(err) {
// console.log(err)
// }
}
return (
<SafeAreaView style={productsCaS.C}>
<View style={productsCaS.warp}>
<Text style={productsCaS.txt}>مواد غذائية</Text>
<ScrollView
onScroll={({ nativeEvent }) => change(nativeEvent)}
showsHorizontalScrollIndicator={false}
pagingEnabled
horizontal
style={productsCaS.box}
bouncesZoom={true}
>
{dataProduct.forEach((product) => {
return(
<View style={{ width: phoneH.W(45), marginRight: phoneH.W(2.5) }}>
<TouchableOpacity style={productsCaS.box}>
<Image
source={{ uri: 'https://huzurshops.com/wp-content/uploads/2013/06/3a-300x300.jpeg' }}
style={productsCaS.img}
/>
<Button
buttonStyle={productsCaS.btn}
containerStyle={productsCaS.btn}
icon={
<Ikon is={'SimpleLineIcons'} i={'handbag'} s={24} />
}
iconRight={true}
title='اضافة الى السلة'
titleStyle={productsCaS.btnTxt}
/>
</TouchableOpacity>
<TouchableOpacity style={productsCaS.title}>
<Text style={productsCaS.titleTxt}> {product.title.rendered}</Text>
</TouchableOpacity>
<View style={productsCaS.iconBox}>
<Ikon is={'Entypo'} i={'star'} s={20} c={'yellow'} />
<Ikon is={'Entypo'} i={'star'} s={20} c={'yellow'} />
<Ikon is={'Entypo'} i={'star'} s={20} c={'yellow'} />
<Ikon is={'Entypo'} i={'star'} s={20} c={'yellow'} />
</View>
<Text style={productsCaS.price}>{product._price}</Text>
</View>
)})}
</SafeAreaView>
</View>
解决方案
推荐阅读
- excel - Excel - 自动工作表切换循环**仅限特定工作表**
- r - Quantmod getDividends 的解决方法,显然已于 2020 年 1 月 16 日停止工作
- bash - 如何强制在 shell 脚本中只使用一个标志?
- grpc - gRPC go:在服务级别应用拦截器
- sql - 我想在 SQL Server 表的特定列中找到最长的 varchar
- reactjs - JEST 测试用例应该使用 try-catch 失败
- c - C - XMPLAB DDRD/DDRB 不可分配错误
- rundeck - rundeck - 从 linux 上的命令行提交作业?
- javascript - 计算属性中的 Vuex 和嵌套对象导致反应性问题
- c# - C# 数据流块可以拥有状态吗?