Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

100
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!