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

185
Visualizações
Using React useState in array.map for multiple elements

I'm trying to set up multiple useStates on dynamically created elements inside an array.map. Every element inside of the array.map should use the same useState, but have its proper value assigned. For example the array.map has 3 different elements, which all should have "focus" set to false. When clicked, the clicked element's useState should change to true, the others should remain false.

    const [focus, setFocus] = useState(false);
    { projects.map((project, index) => (
        <div className={` ${focus ? "focus" : ""} `} key={project.id}>
            <div className="cover" to={"/work/" + project.slug} onClick={ () => setFocus(true) }>
            </div>
        </div>
    ))}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to store the id of the project that should have the focus. A single boolean state property isn't going to work.

e.g.

const [focusedProject, setFocusedProject] = useState(undefined);
...
onClick={() => setFocusedProject(project.id)}

then

<div className={` ${focusedProject === project.id ? "focus" : ""} `} key={project.id}>
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