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

166
Vistas
Conditional styling in react map

I only want to show the display block on the hovered item. but when I hover it shows on every item in a map function. what I'm doing wrong.

image basically, I just want to show hovered movie item's title. for now, it shows every movie when I hover.

MovieList.js

  const [popular, setPopular] = useState([]);
  const [hover, setHover] = useState(false);
  
  return (
    <>
      <div className="movie-list">
        <h2 style={{ fontSize: "49px", marginLeft: "60px" }}>What's Popular</h2>

        <div className="popular">
          {popular.map((pop, index) => (
            <>
              <div className="movie" key={index}>
                <div className="tot" onMouseEnter={() => setHover(true)}>
                  <h4
                    id="pop-title"
                    style={{ display: hover ? "block" : "none" }}
                    key={index}
                  >
                    {pop.title}
                  </h4>
                </div>
                <img
                  src={"https://image.tmdb.org/t/p/w500" + pop.poster_path}
                  id="movie-img"
                />
              </div>
            </>
          ))}
        </div>
      </div>
    </>
  );
};

export default MovieList;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The same hover state variable is used for all your movies, so when it becomes true, all the movies are affected by the change. You could use something like an object instead of just a boolean to store one hover state per movie.

Another problem with your code that isn't helping:

You are missing a unique key prop on each item of the map (it must be on the direct child).

Solution 1: Remove the Fragment

Everything is already under one div so you don't need the React Fragment (<>) in that case. Also, you might wanna use something unique to the current map item other than the index in the array.

{popular.map((pop) => (
    <div className="movie" key={pop.somethingUnique}>
        <div className="tot" onMouseEnter={() => setHover(true)}>
            <h4 id="pop-title" style={{ display: hover ? "block" : "none" }}>
                {pop.title}
            </h4>
        </div>
        <img
            src={"https://image.tmdb.org/t/p/w500" + pop.poster_path}
            id="movie-img"
        />
    </div>
))}

Solution 2: Set the key on the Fragment

{popular.map((pop) => (
    <Fragment key={pop.somethingUnique}>
        <div className="movie">
            <div className="tot" onMouseEnter={() => setHover(true)}>
                <h4 id="pop-title" style={{ display: hover ? "block" : "none" }}>
                    {pop.title}
                </h4>
            </div>
            <img
                src={"https://image.tmdb.org/t/p/w500" + pop.poster_path}
                id="movie-img"
            />
        </div>
    </Fragment>
))}
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