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

183
Visualizações
How update dynamically the className according its content in Next.js?

I try to set color dynamically according the content, for example if the value is android the background should be green and when it is ios, it should be blue. I want to do this with a generic function for all the Next.js project (I use Tailwind CSS by the moment).

This is the generic function to get the specific color according the content (in this case tag of a post).

export function getColor(tag) {
  switch (tag) {
    case "android":
    case "androidx":
    case "espresso":
    case "retrofit":
      return "bg-green-500"
    case "ios":
    case "facebook":
      return "bg-blue-500"
    case "angular":
    case "java":
    case "javascript":
    case "javafx":
    case "hms":
      return "bg-red-500"
    case "kotlin":
      return "bg-purple-500"
    case "firebase":
      return "bg-yellow-500"
    case "swift":
    case "swiftui":
    case "ubuntu":
      return "bg-orange-500"
  }
  return "bg-gray-500"
}

And this is the way I set the className.

<span
  className={`text-white uppercase ${getColor(post.tags?.[0])} inline-flex items-center px-2 rounded text-sm font-semibold font-mono`}>
  {post.tags?.[0]}
</span>

Apparently this should works, but it doesn't. Even sometimes this works and sometimes not. Previously I had this function on each file where I need it and it works but this is not the idea. I want to have a generic function (separated file) for all the project.

What is the right way to handle this? There is another way to update the className? What's the best approach for this? Thank you very much.

Update:

I found the solution thanks to @stickyuser, my generic function is in lib/util.js file, and this is no present in the tailwind.config.js. I added this path to the tailwind config and this works.

module.exports = {
  content: [
    "./pages/**/*.{js,ts,jsx,tsx}",
    "./components/**/*.{js,ts,jsx,tsx}",
    "./lib/**/*.{js,ts,jsx,tsx}",
  ],
...
almost 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If it was working when the logic was included in the files, but not now, then it sounds like the file containing the logic is not included in the content setting for your tailwind.config.js.

Try updating the content setting to include the logic file so Tailwind knows to add those class names (e.g. bg-green-500, bg-blue-500 etc) to its output css.

almost 4 years ago · Santiago Trujillo Relatório

0

it's more convenient to use clsx for dynamic classes, it will look like className={clsx('some-class', areThereData && 'some-other-class', getColor(...))}.

You can use console.log or the debugger statement to debug; probably when it doesn't work, you don't pass the correct value to the function

almost 4 years ago · Santiago Trujillo 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