Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

240
Vistas
How to get response of a function in another file

I have two files named actions.js and vip.js. I have declared a function fetchVip in action file and imported it in vip file which will populate text element when screen is loaded. I want to access fetchVip response in vip file but i get undefined like this LOG useeffect undefined. While respone works as expected in action file.My code is below.

Vip File

import {fetchVip} from '../../Store/action';

const Vip = props => {
  useEffect(() => {
    console.log('useeffect', fetchVip());
  }, [fetchVip()]);

action file

export const fetchVip = () => {
  axios
    .post(`${baseUrl}/get-vips`)
    .then(function (response) {
      return response.data;
    })
    .catch(function (error) {
      console.log('error', error);
      return {type: 'ERROR', payload: error};
    });
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

fetchVip does not return anything, so you have to add a return statement here first:

export const fetchVip = () => {
  return axios
    .post(`${baseUrl}/get-vips`)

or remove the curly braces, then it will return as well.

export const fetchVip = () => axios
    .post(`${baseUrl}/get-vips`)
...
      return {type: 'ERROR', payload: error};
    })

Now it will return a promise. That means that the result will not be there right away, but at some point later in time. Therefore, if you want to use it in the useEffect, you have to await for the result to arrive.

you could to this with the ES6 syntax:

  useEffect(() => {
    const getVip = async () => {
      const vip = await fetchUsers();
      console.log(vip)
      //now you can do something with it
    };

    getVip();

  }, [fetchVip]);

or the promise-then syntax:

  useEffect(() => {
    fetchVip().then(result => {
      console.log(result);
      //do something with the result
    })
    
  }, [fetchVip]);

This is wrong btw. remove the (). You want to check for the function here, not the result of the function.

}, [fetchVip()]);
about 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda