I have the below:
<div className="grid md:grid-cols-2 sm:grid-cols-2 grid-cols-1 gap-16 mt-24 px-4">
{status === "success" &&
delve(data, "restaurants") &&
data.restaurants.map((restaurant, index) => (
<div className=""> <---------------- i.e.
<RestaurantCard
{...restaurant.attributes}
locale={locale}
key={index}
/>
</div>
))}
</div>
I want to apply a class to every other element within my .map
function. Reason is, as opposed to standard side by side grid column, I am creating a checkerboard type layout, so every other item I will need to margin up higher. How could I apply such a style to only other item in data.restaurants.map((restaurant, index) => (
?
4
Answers
I hate this answer because this does not make sense to me.
You can apply conditional
className
according to your index.For example,
If is the same style, add your class in the
div
inside of themap
Also if you need to make some logic to apply the class
Also you can apply the class to your component
And then apply that style in the component file
You can use the modulus operator (%) to check if the index is even or odd. Based on that, you can conditionally apply different classes to alternate elements, creating a pattern or checker switching logic.
Based on your example: