简体   繁体   English

如何根据 Next.js 中的内容动态更新 className?

[英]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.我尝试根据内容动态设置颜色,例如,如果值为android背景应该是绿色,当它是ios时,它应该是蓝色。 I want to do this with a generic function for all the Next.js project (I use Tailwind CSS by the moment).我想为所有 Next.js 项目使用通用 function 来执行此操作(我目前使用 Tailwind CSS)。

This is the generic function to get the specific color according the content (in this case tag of a post).这是通用的 function 根据内容(在本例中为帖子的标签)获取特定颜色。

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 .这就是我设置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.以前我在每个需要它的文件上都有这个 function 并且它可以工作,但这不是想法。 I want to have a generic function (separated file) for all the project.我想为所有项目提供一个通用的 function (分隔文件)。

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.非常感谢。

it's more convenient to use clsx for dynamic classes, it will look like className={clsx('some-class', areThereData && 'some-other-class', getColor(...))} .clsx用于动态类更方便,它看起来像className={clsx('some-class', areThereData && 'some-other-class', getColor(...))}

You can use console.log or the debugger statement to debug;可以使用console.log或者debugger语句进行调试; probably when it doesn't work, you don't pass the correct value to the function可能当它不起作用时,您没有将正确的值传递给 function

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 .如果在文件中包含逻辑时它正在工作,但现在没有,那么听起来包含逻辑的文件未包含在tailwind.config.jscontent设置中。

Try updating the content setting to include the logic file so Tailwind knows to add those class names (eg bg-green-500 , bg-blue-500 etc) to its output css.尝试更新content设置以包含逻辑文件,以便 Tailwind 知道将这些 class 名称(例如bg-green-500bg-blue-500等)添加到其 output ZC7A628CBA22E28EBA2617B5F5C6。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM