将尾风班级作为React的道具
我正在创建一个可重复使用的组件按钮,我想通过该按钮将两个尾风类作为道具,并动态使用它们。
这是我的组成部分:
function Button({ primary, secondry, label, onClick }) {
return (
<button
onClick={(e) => onClick(e)}
className={`bg-${primary} py-0.5 px-3 rounded text-white font-semibold text-xl border-2 border-${primary} hover:bg-${secondry} hover:text-${primary} cursor-pointer duration-300`}
>
{label}
</button>
);
}
这是我使用该组件的方式:
<Button
label={"Cancel"}
primary="red-700"
secondry="zinc-900"
onClick={(e) => navigate("/customers")}
/>
但是,这些类并未应用。这就是外观:
I am creating a reusable component button to which I want to pass two Tailwind classes as props and use them dynamically.
Here is my component:
function Button({ primary, secondry, label, onClick }) {
return (
<button
onClick={(e) => onClick(e)}
className={`bg-${primary} py-0.5 px-3 rounded text-white font-semibold text-xl border-2 border-${primary} hover:bg-${secondry} hover:text-${primary} cursor-pointer duration-300`}
>
{label}
</button>
);
}
This is how I am using the component:
<Button
label={"Cancel"}
primary="red-700"
secondry="zinc-900"
onClick={(e) => navigate("/customers")}
/>
However, the classes are not being applied. This is how it looks:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试传递整个字符串
bg-red-700
这样这样
Try to pass the whole string
bg-red-700
like this like thisAnd use it like
使用
tailwind-merge
和clsx
软件包您可以实现此处:创建一个实用程序
cn
这样的功能,然后在需要使用的任何地方导入此信息。这样使用:
Using
tailwind-merge
andclsx
packages you can achieve this:Create a utility
cn
function like this and then import this wherever you need to use it.Use it like this:
尝试像这样的
className = {
bg- ['$ {priendar}'] ....
}try to pass it like this
className={
bg-['${primary}'] ....
}