第二个 useEffect 块在第一个收到任何数据之前运行。这会导致第二个块返回一个错误和一个空数组。
我试过使用 async/await,因为这在过去为我解决了这样的问题。但是,它似乎在这里没有影响。
const [session, setSession] = useState("");
const [champions, setChampions] = useState([]);
useEffect(() => {
axios.get(`http://api.paladins.com/paladinsapi.svc/createsessionJson/${devId}/${generateSignature('createsession')}/${moment.utc().format('YYYYMMDDHHmmss')}`).then((response) => {
setSession(response.data.session_id);
console.log(session);
}).catch((error) => {
console.log(error);
})
}, [])
useEffect(() => {
axios.get(`http://api.paladins.com/paladinsapi.svc/getchampionsJson/${devId}/${generateSignature('getchampions')}/${session}/${moment.utc().format('YYYYMMDDHHmmss')}/1`).then((response) => {
setChampions(response.data);
console.log(champions);
}).catch((error) => {
console.log(error);
})
}, []);
它应该向冠军返回一个对象数组,但由于它没有收到会话 ID,因此 api 调用不是将冠军保持为空数组的正确地址。
holdtom
翻阅古今
相关分类