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

218
Visualizações
How to dynamically set a backGroundColor to a card depending on its category with Nextjs

I have a nextjs app where I want to give different backGroundColor to cards relatively to their category.

I tried this :

card.js

import { React, useEffect } from "react"
import Link from "next/link"
import NextImage from "./image"

const animColor = "#FF006E";
const decorColor = "#FFBE0B";
const illuColor = "#3A86FF";

const Card = ({ article, categories }) => {

  useEffect(() => {
    const overlay = document.getElementById("overlay");

    article.attributes.categories.data.forEach(category => {
        console.log(category.attributes.slug)
        if (category.attributes.slug === "animation") {
          overlay.style.backgroundColor = animColor;
        }
        if (category.attributes.slug === "decor") {
          overlay.style.backgroundColor = decorColor;
        }
        if (category.attributes.slug === "illustrations") {
          overlay.style.backgroundColor = illuColor;

        }

    });

  }, []);

  return (
    <Link href={`/article/${article.attributes.slug}`}>
      <a className="hover:no-underline">
        <div className="card relative grid mb-3 md:mb-0">
          <div className="media">
            <NextImage image={article.attributes.image} />
          </div>
          <div id="overlay" className="overlay-color"></div>
          <div className="card-body flex items-end justify-center pb-5">
            <p className="text-xl px-2 uppercase text-center text-white">
              {article.attributes.title}
            </p>
          </div>
        </div>
      </a>
    </Link>
  )
}

export default Card

articles.js

import React from "react"
import Card from "./card"

const Articles = ({ articles, categories}) => {
 
  return (
    <>
      <div className="md:grid grid-cols-3 gap-5 justify-center place-content-center">
            {articles.map((article, i) => {
              return (
                <Card
                  article={article}
                  categories={categories}
                  key={article.attributes.slug}
                />
              )
            })}
          </div>
    </>
  )
}

export default Articles

I might complicate the task more than it is really, but I'm kind of lost in my head. I'm try to catch the category of each card and if category.slug === [each categories] then It change backGroundColor style.

With this code it changes only the color of the first card. I guess there is an error in the iteration process I chose.

Any idea to solve it and maybe do it better ?

about 4 years ago · Juan Pablo Isaza
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