繁体   English   中英

从反应函数返回数组我哪里出错了

[英]Where am I going wrong with returning an array from a react function

所以我有一个反应函数,它是一个 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) 时,我变得未定义。

您是从回调函数返回,而不是从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)
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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