DEV Community

Muhammad Awais
Muhammad Awais

Posted on

Applying Classes Conditionally in React

in this example, I have an iterated list of data and i want to apply a class if the index of iteration is even.

so, here is a solution,

data.map((item, index) => {
   return (
     <p key={index} className={ index%2==0 ? "filled" : "no-filled" }> 
       {item.name}
     </p>
   );
})
Enter fullscreen mode Exit fullscreen mode

Top comments (0)