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

222
Vistas
React / Axios: Cannot read properties of undefined (reading '0')

I can see array with objects of data "fees" perfectly but if i wanna see a single value like fees[0]?.firstFee i get this error:

Uncaught TypeError: Cannot read properties of undefined (reading '0')

i thought ? operator would avoid this but seems like not...

const [fees, setFees] = useState();

useEffect(() => {
    const getFees = async () => {
      const res = await axios.get('/api/feesettings', {
        headers: { Authorization: token },
      });
      setFees(res.data);
    };

    getFees();
  }, []);

  console.log(fees);
  console.log(fees[0]?.firstFee );
about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

The reason why you are getting an error on every render is because, you are passing no value to the state, so by default fees is undefined

const [fees, setFees] = useState();

and when you reach this line of code, you are trying to access fees[0] as if fees is an array, it is not that's why it's giving you an error Cannot read properties of undefined (reading '0')

console.log(fees[0]?.firstFee);

So change to this line of code,

const [fees, setFees] = useState([]);

if (fees.length > 1) {
    console.log(fees[0]?.firstFee);
}
about 4 years ago · Santiago Gelvez 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