简体   繁体   中英

I'm getting the first response an empty list

I'm getting data from API like this:

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. I tried to use useEffect because setSoftwares is asynchronous, but it didn't help. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM