Skip to content
Advertisement

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

So I have a react function that is a api call ad gets some data, I want to just return the status of that back to the parent component.

Here is my code

Parent component function

//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)
    }

Child function

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)
    });  

I am getting undefined when console.log(returned) is called.

Advertisement

Answer

You are returning from the callback function and not from the SchedulePayment . You should add return before your api call in order to work. Or in a nicer way

export default async function SchedulePayment(data){
  try {
    const res = axios.post(<api-url>, data)
    return res
  } catch (err) {
    console.log(err)
    return(err)
  }
}
Advertisement