const [colorTheme, setColorTheme] = useState("#8D9093");
const getColorTheme = (websiteName) => {
const website = websiteData.find(
(site) => site.name.toLowerCase() === websiteName.toLowerCase()
);
if (website) {
console.log(website.colorTheme);
setColorTheme(website.colorTheme);
} else {
console.log("Website not found");
setColorTheme("#8D9093");
}
};
I am fetching colorTheme from a JSON file.
我正在从JSON文件中获取colorTheme。
{
"name": "YouTube",
"url": "https://www.youtube.com/results?search_query=",
"colorTheme": "#FF0000"
},
And this is one of the examples of how I'm using colorTheme in tailwind
这是我如何在顺风中使用ColorTheme的一个例子
className={`h-fit w-fit px-2 rounded-xl bg-[${colorTheme}] shadow-[0_0_5px_${colorTheme},0_0_15px_${colorTheme}] text-white`}
The color changes only for the YouTube's colorTheme(#FF0000) and does not change for anything else. But I can that being set for every website in the console. How can it change of one and not for others.
只有YouTube的ColorTheme(#FF0000)的颜色才会更改,其他任何内容都不会更改。但我可以在控制台中为每个网站设置。怎么可能只改变一个人而不改变其他人。
Its changing for Youtube
And not for others
这对YouTube来说是改变,对其他人来说不是
更多回答
Does changing the setState to setColorTheme(() => _whatever_);
make a difference?
将setState更改为setColorTheme(()=>_Anywhere_);会有什么不同吗?
Is the problem that state isn't updating, or is the problem that the styling isn't doing what you expect? The pictures linked to suggest that state is working just fine. Can you provide a runnable minimal reproducible example which demonstrates the problem?
问题是状态没有更新,还是样式没有按照您的预期进行?链接到的图片表明,这个国家运行得很好。您能提供一个可运行的、可重现的最小示例来说明该问题吗?
@David The state is changing as you can see in the console. But it works only for one value (#FF0000). but doesn't for other values. When I set state to those other values directly (eg setState(#F72617)), it works. So there is not problem with the styling.
@David,正如您在控制台中看到的那样,状态正在发生变化。但它只对一个值(#FF0000)有效。但对于其他价值观却并非如此。当我直接将STATE设置为其他值时(例如setState(#F72617)),它起作用了。所以造型上没有问题。
优秀答案推荐
As per the documentation:
根据文档:
The most important implication of how Tailwind extracts class names is that it will only find classes that exist as complete unbroken strings in your source files.
If you use string interpolation or concatenate partial class names together, Tailwind will not find them and therefore will not generate the corresponding CSS:
Don’t construct class names dynamically
<div class="text-{{ error ? 'red' : 'green' }}-600"></div>
In the example above, the strings text-red-600
and text-green-600
do not exist, so Tailwind will not generate those classes.
Instead, make sure any class names you’re using exist in full:
Always use complete class names
<div class="{{ error ? 'text-red-600' : 'text-green-600' }}"></div>
Consider using the style
attribute instead:
请考虑改用style属性:
<div
className="h-fit w-fit px-2 rounded-xl text-white"
style={{
backgroundColor: colorTheme,
boxShadow: `0 0 5px ${colorTheme}, 0 0 15px ${colorTheme}`,
}}
>
更多回答
我是一名优秀的程序员,十分优秀!