Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda