Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!