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

187
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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