[英]I'm getting the first response an empty list
I'm getting data from API like this:我从 API 获取数据,如下所示:
const [software, setSoftware] = useState([]);
const id = match.params.id;
useEffect(() => {
fetch(`http://127.0.0.1:8000/api/software/${id}/`)
.then(response => response.json())
.then(data => {
setSoftware(data)
})
}, [id]);
First response is an empty list, but the next response is my list from API.第一个响应是一个空列表,但下一个响应是我来自 API 的列表。 I tried to use useEffect because setSoftwares is asynchronous, but it didn't help.我尝试使用 useEffect 因为 setSoftwares 是异步的,但它没有帮助。 So how can I get only my list?那么我怎样才能只得到我的清单呢?
I think you are sending incorrect id
for the first time, try to console.log(id)
it and check-in the console if id
is valid or not.我认为您是第一次发送不正确的id
,请尝试对其进行console.log(id)
并在id
是否有效时签入控制台。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.