所以我有一個反應函式,它是一個 api 呼叫廣告獲取一些資料,我只想將它的狀態回傳給父組件。
這是我的代碼
父組件函式
//this is called via a button with a state passed down to the function
async function scheduleParent(){
const returned = await ScheduleChild(data)
console.log(returned)
}
子功能
export default async function ScheduleChild(data){
await axios({
method: "POST",
url: //myapi,
data: {data}
}).then(res => {
console.log(res)
return(res)
}).catch(err => {
console.log(err)
return(err)
});
呼叫 console.log(returned) 時,我變得未定義。
uj5u.com熱心網友回復:
您是從回呼函式回傳,而不是從SchedulePayment . 您應該在 api 呼叫之前添加 return 才能作業。或者以更好的方式
export default async function SchedulePayment(data){
try {
const res = axios.post(<api-url>, data)
return res
} catch (err) {
console.log(err)
return(err)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/337244.html
標籤:javascript 反应
上一篇:當我嘗試添加API密鑰時,出現錯誤:TypeError:items.mapisnotafunction
下一篇:使用鉤子的全域React函式
