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

102
Vistas
React - strange data looping behavior

I have an array of months that I'm displaying along the x-axis of a graph. Using date-fns package to format the dates.

In this.props.data:

[
    {
        "month": 11,
        "year": 2020,
    },
    {
        "month": 12,
        "year": 2020,
    },
    ...
]

A method creates an array of DOM nodes:

  renderXAxis() {
    const items = [];

    if (this.props.data.length) {
      for (const data of this.props.data) {
        const month = formatDate(setMonth(new Date(), data.month - 1), 'MMM');

        items.push(
          <div className="Chart__x-axis__item" key={month + data.year}>
            <div className="Chart__x-axis__item__month">{month}</div>
          </div>,
        );
      }
    }

    return items;
  }

In my render method, I call:

<div className="Chart__x-axis">{this.renderXAxis()}</div>

It renders the xAxis with all items displaying the same month. What is weird to me is that if I change the DOM node that gets pushed to the item array to:

    items.push(
      <div className="Chart__x-axis__item" key={month + data.year}>
        <div className="Chart__x-axis__item__month">{`${month}`}</div>
      </div>,
    );

or

    items.push(
      <div className="Chart__x-axis__item" key={month + data.year}>
        <div className="Chart__x-axis__item__month">{formatDate(setMonth(new Date(), data.month - 1), 'MMM')}</div>
      </div>,
    );

then the months all render as they should. I don't understand why or what is going on..

about 4 years ago · Juan Pablo Isaza
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