【发布时间】:2021-05-23 19:05:18
【问题描述】:
useEffect(async () =>
{
const fetchData = async () =>
{
const result = await axios("")
setposts(result.data)
}
fetchData();
}, [])
渲染:
posts.map(post => <li>{post.name}</li>)
Json 文件:
[
{
"id": 1,
"vendor": "Gap",
"name": "Men's Pullover Sweatshirt",
"image_src": ["https://cdn.shopify.com/s/files/1/0455/2176/4502/files/Sweater_0.jpg?v=1603266982"],
"price": "74",
"tag": "T-shirt",
"compare_at_price": "200",
"options": [
{
"id": "1010",
"name": "Size",
"value": "US 8"
}, {
"id": "1011",
"name": "Size",
"value": "US 9"
}, {
"id": "1012",
"name": "Size",
"value": "US 10"
}, {
"id": "1013",
"name": "Size",
"value": "US 11"
}, {
"id": "1014",
"name": "Size",
"value": "US 13"
}
]
},
我面临着从 json 文件中检索数据的问题,就像我无法渲染数据,所以请帮助解决这个问题
映射数据时出错
我面临着从 json 文件中检索数据的问题,就像我无法渲染数据,所以请帮助解决这个问题
【问题讨论】:
-
1.更多代码。 2. 发布错误信息。
-
我猜你需要获取方法来访问你的数据,比如 await axios.get(baseURL)....
-
@codemonkey 错误是:TypeError:posts.map 不是函数
-
@AhmetSuhanOka 我从 Json 获取数据我在渲染数据时遇到问题
标签: javascript json reactjs async-await axios