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

156
Views
How to map with unique value in pattern

I have an array of contents that I am fetching from headless cms. I am putting them on grid and trying to make them start at different column, but I am struggling to find a way to go about it.

If I were to just render the items it would look like this, everything starts at column 1:

<div className="relative grid grid-cols-3">
  {projects.map((project) => (
        <div
          className={"col-start-1"}
          key={project.data.title}
        >
          <ProjectThumbnail/>
        </div>
      ))}
</div>

And this is what I am trying to achieve:

 <div className="relative grid grid-cols-3">

  <div className="col-start-1">
    <ProjectThumbnail/>
  </div>

  <div className="col-start-3>
    <ProjectThumbnail/>
  </div>

  <div className="col-start-2>
    <ProjectThumbnail/>
  </div>

  <div className="col-start-1">
    <ProjectThumbnail/>
  </div>

  <div className="col-start-3>
    <ProjectThumbnail/>
  </div>

  <div className="col-start-2>
    <ProjectThumbnail/>
  </div>

...

</div>

Thank you

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const classes = ["col-start-1", "col-start-3", "col-start-2"];

const projects = [
    {data: {title: 'Title 1'}},
    {data: {title: 'Title 2'}},
    {data: {title: 'Title 3'}},
    {data: {title: 'Title 4'}},
    {data: {title: 'Title 5'}},
    {data: {title: 'Title 6'}},
    {data: {title: 'Title 7'}}
];

function ProjectThumbnail({project}) {

    return <span>[{project.data.title}]</span>
}

function Grid({projects}) {

    return (
        <div className="relative grid grid-cols-3">
            {projects.map((project, idx) => (
                <div
                    className={classes[idx % classes.length]}
                    key={project.data.title}
                >
                    <ProjectThumbnail project={project}/>
                </div>
            ))}
        </div>
    );
}

function App() {

    return <Grid projects={projects}/>
}
about 4 years ago · Juan Pablo Isaza Report

0

Assuming you want the column to be 1,3 and 2, in that order, and repeating indefinitely, then you could use the index from map, and calculate the proper column number from that using remainders. Like this:

<div className="relative grid grid-cols-3">
  {projects.map((project, i) => (
        <div
          className={`col-start-${i % 3 === 0 ? 1 : (i % 3 === 1 ? 3 : 2)}`}
          key={project.data.title}
        >
          <ProjectThumbnail />
        </div>
      ))}
</div>
about 4 years ago · Juan Pablo Isaza Report
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!