当我开始我的项目它不加载某些tailwind 类名称时,这个类名来自一个react 属性,我附上了一个显示错误的视频,我想知道如何让它加载 colored颜色 ,即使在类名中使用三元运算符

一段展示我的问题的视频https://youtu.be/RcLbC5kA_fQ

呼叫组件

<CardCampanha 
                    isPrivate={true}
                    age={"+18"}
                    title={"Nome da campanha"}
                    slots={"9/10"}
                    active={'#0FF0A0'}
                    genders={[
                                {title: "Fantasia", color: "#817EEF"}, 
                                {title: "Oneshot", color: "#EFAE7E"},
                                {title: "Gay", color: "#EF7EE4"}
                            ]}
                        imgURL={"/imagemcampanha/campanha.jpg"}
                />

该组件

export default function CardCampanha({active, title, slots, genders, age, isPrivate, notActive, imgURL}){

        console.log(genders)
    
    genders.map(gender => {
        console.log(gender)
    })

    return(
        <div className='h-[26.01vh] w-[37.96vh] bg-white rounded-[1vh]'>
            <div className='h-[16.4815vh] w-[37.963vh] bg-black rounded-[1vh] rounded-b-none'>
                <div className='w-[3.5185vh] h-[3.4259vh] bg-black absolute flex items-center justify-center  rounded-[0.9259vh] mt-[1.5vh] ml-[2.5vh] z-10'>
                    <p className='font-nonito text-[1.8519vh] text-white'>{age}</p>
                </div>
                    <div className={`w-[2.037vh] h-[2.037vh] bg-[${active}] absolute flex items-center justify-center rounded-full mt-[1.5vh] ml-[34vh] z-10`}>
                </div>
                
                <div className=" w-full h-full flex items-center justify-center bg-[#C6C6C6] rounded-t-[1vh]">

                    {imgURL ? 
                            <img className={`h-full w-full ${notActive ? "saturate-0 z-10" : ""} object-cover rounded-t-[1vh] z-0`} src={imgURL} alt="" />
                        :
                            
                            <svg width="91" height="91" viewBox="0 0 91 91" fill="none" xmlns="http://www.w3.org/2000/svg" xlink="http://www.w3.org/1999/xlink">
                                <rect x="0.40625" y="0.322754" width="90.2732" height="90.2732" fill="url(#pattern0)"/>
                                <defs>
                                <pattern id="pattern0" patternContentUnits="objectBoundingBox" width="1" height="1">
                                <use href="#image0_783_145" transform="scale(0.0078125)"/>
                                </pattern>
                                <image id="image0_783_145" width="128" height="128" href="data:image/png;base64,mybase64"/>
                                </defs>
                            </svg> 

                    }

                    
                </div>
            </div>

            <div className=' flex flex-col'>
                <div className='h-1/2 flex justify-between pl-[2.9vh] pr-[2vh] pt-[1.7vh] '>
                    <div className='h-full flex items-center justify-center gap-[0.5vh] '>
                        {isPrivate &&
                            <div className='w-[1.8vh] pb-[0.4vh]'>
                                <img src="/icons/cadeado.ico" alt="" />
                            </div>
                        }
                        <p className='font-nonito text-[1.8519vh] truncate w-[23vh]'>{title}</p>
                    </div>

                    <div className='h-full flex items-center justify-centers gap-[0.5vh] '>
                        <div className='w-[2.2vh] pb-[0.2vh]'>
                            <img src="/icons/pessoas.png" alt="" />
                        </div>
                        <p className='font-nonito text-[1.8519vh] '>{slots}</p>
                    </div>
                </div>
                <div className='h-1/2 flex pl-[2.5vh] pt-[1vh] gap-[0.5vh]'>
                    {
                        genders.map(gender => (
                            <div className={`bg-[${gender.color}]  rounded-r-[1.8519vh] rounded-l-[1.8519vh] px-[1.5vh] py-[0.2vh] flex items-center justify-center`}>
                                <p className='font-inter text-[1.2037vh] text-white'>{gender.title}</p>
                            </div>
                        ))
                    }
                </div>
            </div>
        </div>
    )
}
                

推荐答案

the documentation:

TailWind提取类名的最重要的含义是,它将只找到在源文件中以complete unbroken strings的形式存在的类.

如果使用字符串内插或将部分类名连接在一起,TailWind将找不到它们,因此也不会生成相应的CSS:

Don’t construct class names dynamically

<div class="text-{{ error ? 'red' : 'green' }}-600"></div>

在上面的示例中,字符串text-red-600text-green-600不存在,因此TailWind将不会生成这些类. 相反,请确保您正在使用的所有类名都完整存在:

Always use complete class names

<div class="{{ error ? 'text-red-600' : 'text-green-600' }}"></div>

您可以考虑:

  • 在props 中使用完整的类名,如下所示:
    <CardCampanha … active="bg-[#0FF0A0]" />
    
    export default function CardCampanha({active, title, slots, genders, age, isPrivate, notActive, imgURL}){
      // …
      <div className={`… ${active} …`}>
    
  • 使用style属性,如:
    <CardCampanha … active="#0FF0A0" />
    
    export default function CardCampanha({active, title, slots, genders, age, isPrivate, notActive, imgURL}){
      // …
      <div … style={{ backgroundColor: active }}>
    

Reactjs相关问答推荐

无法覆盖MUI工具栏上的左右填充,除非使用!重要

从app.js导航,这是在BrowserRouter包装之外

REACTJS:在Reaction中根据路由路径更改加载器API URL

如何在单击行中的图标时避免选中ionic 复选框?

是否在Extra Reducer完成作业(job)时触发createListenerMiddleware?

在Reaction中单击并显示子组件延迟/动画

REACTION 18功能组件正在开发中的S

在 React 中,为什么不将组件和钩子结合起来呢?

React 错误: 只能用作 元素的子元素,从不直接渲染.请将您的 包裹在

在reactjs中刷新页面时丢失状态值

列表中的每个子项都应该有一个唯一的keyprops .在react 应用程序中

为什么 useEffect 的初始化对于加载 localStorage 不起作用?

无法设置 null 的属性(设置src)

字段是必需的,当它没有定义时

如何禁用 redux-toolkit 的不可序列化值测试警告?

React Native map 中的初始zoom 级别

使用 React.UseEffect 从 api 获取但我的清理返回不起作用

谁能根据经验提供有关 useEffect 挂钩的更多信息

在 redux 工具包中使用 dispatch 发送多个操作

gtag:为什么谷歌分析即使没有被授权也会收集数据