更改时尾风颜色未显示

发布于 2025-02-01 04:14:40 字数 858 浏览 3 评论 0 原文

我得到了一个阵列,其颜色已经在tail默认的调色板中定义了,如下所示,

colors: ["red-600", "blue-600", "pink-600", "black", "white", "yellow-600"]

我在数组上映射了,以产生带有阵列中定义的背景颜色的小盒子。

{colors.map((col, i) => (
        <button
          className="cursor-pointer"
          key={i}
        >
          <div
            className={`mt-3 border-2 border-solid border-black bg-${col} w-8 h-8`}
          >

我的问题是,如果我更改数组中的任何颜色(例如将红色-600更改为红色900),尽管这两种颜色都在默认的调色板

这里有可能出错?

i got an array with colors already defined in the tailwind default color palette , as follows

colors: ["red-600", "blue-600", "pink-600", "black", "white", "yellow-600"]

I mapped over the array to produce small boxes with a background color thats defined in the array.

{colors.map((col, i) => (
        <button
          className="cursor-pointer"
          key={i}
        >
          <div
            className={`mt-3 border-2 border-solid border-black bg-${col} w-8 h-8`}
          >

enter image description here

my issue is if i change any of the colors in the array ( like changing red-600 to red-900) it doesnt display despite that both colors are defined in the default color palette

enter image description here

what could possibility be wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉生梦死 2025-02-08 04:14:40

我曾经有一个问题,同时尝试了类似于您当前尝试的东西……我意识到您不能部分将尾风值设置为变量,而是如果您要去为其使用变量。

示例:

严格为 bg-colors制作数组

bgColors: [
  "bg-red-600",
  "bg-blue-600", 
  "bg-pink-600", 
  "bg-black", 
  "bg-white", 
  "bg-yellow-600"
]

现在,您应该能够将它们用作具有整体值而不是部分值的变量。

className={`${col}`} // ${col} === bg-color-###

I at one point had this issue while attempting something very similar to what you're currently trying... I realized that you can't set a Tailwind value partially as a variable, rather it must be the entire value if you're going to use a variable for it.

Example:

make an array strictly for bg-colors

bgColors: [
  "bg-red-600",
  "bg-blue-600", 
  "bg-pink-600", 
  "bg-black", 
  "bg-white", 
  "bg-yellow-600"
]

and now you should be able to use them as variables with the whole value instead of a partial value.

className={`${col}`} // ${col} === bg-color-###
遗心遗梦遗幸福 2025-02-08 04:14:40

首先,您应该将颜色数组设置为 state ,当此数组更改时,该内容使该组件会重新渲染。

第二,如果仅在内部订购颜色数组更改,则不会强迫此组件重新渲染以读取状态更改,因此您应添加另一个状态来强制组件RE - 像这样的胶片[forcechangecolor,setforcechangecolor] = usestate(0)

const [colors, setColors] = useState(["red-600", "blue-600", "pink-600", "black", "white", "yellow-600"])
const [forceChangeColor, setForceChangeColor] = useState(0)

const handleChangeColor = () => {
  setColors([/*set your change colors*/]) // <=== set Colors change in here
  setForceChangeColor(prev => prev + 1) //<=== Make another state change to force this component re-render to re-read colors state
}


{colors.map((col, i) => (
        <button
          className="cursor-pointer"
          key={i}
        >
          <div
            className={`mt-3 border-2 border-solid border-black bg-${col} w-8 h-8`}
          >
        

First, you should set colors array as a state, that thing make this component will re-render when this array change.

Second, if only order inside colors array change, it don't force this component re-render to read your state change, so you should add another state to force component re-render like [forceChangeColor, setForceChangeColor] = useState(0):

const [colors, setColors] = useState(["red-600", "blue-600", "pink-600", "black", "white", "yellow-600"])
const [forceChangeColor, setForceChangeColor] = useState(0)

const handleChangeColor = () => {
  setColors([/*set your change colors*/]) // <=== set Colors change in here
  setForceChangeColor(prev => prev + 1) //<=== Make another state change to force this component re-render to re-read colors state
}


{colors.map((col, i) => (
        <button
          className="cursor-pointer"
          key={i}
        >
          <div
            className={`mt-3 border-2 border-solid border-black bg-${col} w-8 h-8`}
          >
        
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文