每次我最终都会在className之外使用style属性,因为下面的示例都不能将样式应用于我的React元素。你能解释为什么会发生这种情况以及我如何解决这个问题吗?
我已经阅读了文档(https://tailwindcss.com/docs/content-configuration#dynamic-class-names),但我的使用情况是:用户从颜色选择器中选择颜色,然后我根据选择的颜色更改背景。我无法将"bg-[colorValue]"的值传递给每个单独的颜色,所以我必须在之后将该值与"bg-["连接起来。因为我无法将所有颜色映射到完整的类名中。
const red500 = "red-500";
const red600Hex = "#dc2626";
const bgColor = "bg-[" + red600Hex + "]";
const bgColor2 = "bg-[" + "#dc2626" + "]";
function App() {
return (
<>
Hello
Hello
Hello
Hello
Hello
>
);
}
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号