• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

131
Vistas
Where am I going wrong with returning an array from a react function

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.

almost 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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)
  }
}
almost 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda