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

302
Vistas
How to fix error: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead

I'm trying to set the a state with an array of objects but I'm getting the above error and I do not know how to resolve it.

My code:

const [itemsInCart, setItemsInCart] = useState([]);

  useEffect(() => {
    const fetchData = async () => {
      await fetch("https://fakestoreapi.com/products?limit=16")
        .then((res) => res.json())
        .then((data) => {
          const initialItemCount = data.map((item) => {
            return {
              name: item.title,
              qty: 0,
            };
          });
          setItemsInCart(initialItemCount);
        });
    };

    fetchData();
  }, []);

I tried console.log(Array.isArray(initialItemCount)) to check if it is an array and it returned true. So why is it saying to use an array? Thank you

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

0

You can use a breakpoint to see the final value of itemsInCart, to check if this array contains the list of React child component.

From your code, I guess the array only contains the object of {name and qty}, which can not be rendered directly.

You may try parsing these objects to a component to render UI.

Hope this help.

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