[英]How could i solve the problem for Javascript Fetch
Below committed code is axios.下面提交的代码是 axios。 Which is working fine.
哪个工作正常。 But javascript fetch is not working.
但是 javascript 提取不起作用。 Let me know what is the issue in the code.
让我知道代码中有什么问题。 How to fix it.
如何修复它。 Please explain it in a simple way.
请用简单的方式解释一下。 Thanks!
谢谢!
import React, {useEffect, useState} from 'react'
import axios from 'axios'
function Datafetching() {
const [posts, setposts] = useState([])
const getMovieRequest = async () => {
const url = `http://jsonplaceholder.typicode.com/posts`;
const response = await fetch(url);
const responseJson = await response.json()
if (responseJson.data){
setposts(responseJson.data)
console.log(responseJson.data)
}
}
useEffect(() => {
// axios.get('http://jsonplaceholder.typicode.com/posts')
// .then(res => {
// console.log(res)
// setposts(res.data)
// })
// .catch(err => {
// console.log(err)
// })
getMovieRequest()
})
return (
<div>
<ul>{
posts.map(post => <li key={post.id}>{post.title}</li>)
}
</ul>
</div>
)
}
export default Datafetching
responseData
contains the data as array, not responseData.data
: responseData
包含数组形式的数据,而不是responseData.data
:
if (responseJson){
setposts(responseJson)
console.log(responseJson)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.