在React.Js中,随机生成的颜色代码无法在TailwindCSS中接受。

huangapple go评论133阅读模式
英文:

Random generated color code not accepting in TailwindCSS in React.Js

问题

以下是您的消息组件的代码部分的翻译:

import randomColor from "random-color";
import React from "react";
import "../Chat.css";

function Messages({username , msg}) {
  const changeTextColor = randomColor(0.99, 0.99).hexString();
  console.log(changeTextColor);
  return (
    <div className="flex-1 px-5 py-2 chatting_body ">
      <div className="flex flex-col px-4 py-2 bg-slate-600 border rounded-md max-w-fit ">
        <div className={`text-sm font-bold text-[${changeTextColor}] `}>{username}</div>
        <div className="flex justify-between items-end space-x-3 ">
          <p className="text-white text-lg ">{msg}</p>
          <p className="text-gray-400 text-xs " >{new Date().toLocaleTimeString()}</p>
        </div>
      </div>
    </div>
  );
}

export default Messages;

请注意,我只对代码部分进行了翻译,而不包括代码中的注释或其他文本。

英文:

Here is my Messages component :

import randomColor from &quot;random-color&quot;;
import React from &quot;react&quot;;
import &quot;../Chat.css&quot;;

function Messages({username , msg}) {
  const changeTextColor = randomColor(0.99, 0.99).hexString();
  console.log(changeTextColor);
  return (
    &lt;div className=&quot;flex-1 px-5 py-2 chatting_body  &quot;&gt;
      &lt;div className=&quot;flex flex-col px-4 py-2 bg-slate-600 border rounded-md max-w-fit &quot;&gt;
        &lt;div className={`text-sm font-bold text-[${changeTextColor}] `}&gt;{username}&lt;/div&gt;
        &lt;div className=&quot;flex justify-between items-end space-x-3 &quot;&gt;
          &lt;p className=&quot;text-white text-lg &quot;&gt;{msg}&lt;/p&gt;
          &lt;p className=&quot;text-gray-400 text-xs &quot; &gt;{new Date().toLocaleTimeString()}&lt;/p&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
}

export default Messages;

By just console.log(changeTextColor); I'm getting the Hexcode, but tailwind can't able to fetch.

答案1

得分: 0

你无法在Tailwind中使用运行时动态值,对此我建议使用自定义属性。

这是一个示例

<div class="flex-1 px-5 py-2 chatting_body ">
  <div class="flex flex-col px-4 py-2 bg-slate-600 border rounded-md max-w-fit ">
    <div class="text-sm font-bold text-[var(--changeTextColor,black)]">{username}</div>
    <div class="flex justify-between items-end space-x-3 ">
      <p class="text-white text-lg ">{msg}</p>
      <p class="text-gray-400 text-xs ">{new Date().toLocaleTimeString()}</p>
    </div>
  </div>
</div>
<button type="button" class="rounded bg-blue-400 p-2 ">更改颜色</button>
英文:

You can't use runtime dynamic value in tailwind, for this I would recommend using custom properties.

Here is an example.

&lt;div class=&quot;flex-1 px-5 py-2 chatting_body  &quot;&gt;
  &lt;div class=&quot;flex flex-col px-4 py-2 bg-slate-600 border rounded-md max-w-fit &quot;&gt;
    &lt;div class=&quot;text-sm font-bold text-[var(--changeTextColor,black)]&quot;&gt;{username}&lt;/div&gt;
    &lt;div class=&quot;flex justify-between items-end space-x-3 &quot;&gt;
      &lt;p class=&quot;text-white text-lg &quot;&gt;{msg}&lt;/p&gt;
      &lt;p class=&quot;text-gray-400 text-xs &quot;&gt;{new Date().toLocaleTimeString()}&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;button type=&quot;button&quot; class=&quot;rounded bg-blue-400 p-2&quot;&gt;change color&lt;/button&gt;

huangapple
  • 本文由 发表于 2023年3月12日 17:39:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定