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

94
Vistas
React componentDidMount setState but return undefined

I can get list with get axios.get method. then ı can use setState and it works flawlessly. but the return is not true , its return undefined console.log(result) => undefined . How can ı check if setState work fine return true or return false ?

getList = async () => {
  await axios.get("http://localhost:5000/list").then((response) => {
    this.setState(
      {
        copy: response.data.list,
      },
      () => {
        return true;
      },
    );
  });
};

componentDidMount = () => {
  this.getList().then((result) => {
    console.log(result);
  });
};
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your return true statement is in setState's post-set callback. It won't be propagated to the promise that's returned from getList; indeed, you return nothing from that function (you don't even return the Axios promise; if you did return that, you would get the response logged in your console.log, but it would be logged before the setState callback finished), so you get undefined in the console.log.

If you need getList to return a promise that resolves to true once the state has been set, you'll need

getList = () => {
  return new Promise((resolve) =>
    axios.get("http://localhost:5000/list").then((response) =>
      this.setState(
        {
          copy: response.data.list,
        },
        () => resolve(true),
      ),
    ),
  );
};

componentDidMount = () => {
  this.getList().then((result) => {
    console.log(result);
  });
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

The second argument to set state can return true but, it's not going anywhere. You need to somehow use it, example:

const response = await axios.get("http://localhost:5000/list")

return new Promise((resolve) => {
  this.setState({
    copy : response.data.list
  }, () => {
    resolve(true);
  })
})

now the chain will work because you are resolving the promise chain with true instead of returning it from the callback function

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