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

129
Vistas
How to update state with an error message in React?

Hi I was trying to update state depending on the error message caught. My code is:

const [errors,setErrors] = useState("Blah blah");
const verifyIfObject = (object) => {
    try {
        if (object === "object") {
            return true;
        } else {
            const errMsg = `Error: ${object} is not a js object...`;

        throw errMsg;
        }
     } 
    catch (e) {
        console.log(e);
         setErrors(e);
    }
};

When executing the code with error I get the error message in console:

"Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop."

Please advise how to change the code so that error messages can be stored in state

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

a try/catch block needs to be used in an async function. You may simplify your function like this:

const [errors,setErrors] = useState("Blah blah");

const verifyIfObject = (object) => {
     if (object !== "object") {
       setErrors(`Error: ${object} is not a js object...`);
       return false
     }
     return true
};

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