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

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

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