React 组件样式的实现方式哪种更好? 大家都发表一下意见么 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
huijiewei
V2EX    React

React 组件样式的实现方式哪种更好? 大家都发表一下意见么

  •  
  •   huijiewei
    huijiewei 262 天前 2906 次点击
    这是一个创建于 262 天前的主题,其中的信息可能已经有所发展或是发生改变。
    function ToggleButtonRemastered({ isEnabled, children }) { return ( <button className={clsx( isEnabled && "scale-110", "transition-transform duration-200 w-max text-xl" )} > {children} </button> ) } 

    function ToggleButtonClassicEdition({ isEnabled, children }) { return ( <button data-enabled={isEnabled} className="data-[enabled=truel:scale-110 transition-transform duration-200 w-max text-xl" > {children} </button> ) } 
    第 1 条附言    262 天前
    两种各有好处吧

    1. 更直观更符合 JS 概念
    2. headless ui 使用的方式比较多,利用 tw-merge 来实现更加灵活的样式,调用的时候可以覆盖 data-attr 的样式
    17 条回复    2025-02-18 21:18:49 +08:00
    nowtg
        1
    nowtg  
       262 天前 via iPhone
    简单易懂,降低项目复杂度
    FrankFang128
        2
    FrankFang128  
       262 天前
    用 tailwind 就行了,别折腾
    ltaoo1o
        3
    ltaoo1o  
       262 天前
    建议第一种,我之前也是第二种写法,后面要在小程序实现相同的页面,代码改动比较多。
    donaldturinglee
        4
    donaldturinglee  
       262 天前
    做项目用 clsx 或 classnames 这种样式拼接方便维护, 自己项目的话, 你喜欢哪种用哪种
    linkopeneyes
        5
    linkopeneyes  
       262 天前
    第一种,简单直观是最好的
    wakarimasen
        6
    wakarimasen  
       262 天前
    第一种好吧,而且 className 类似的库不是应该可以这样写吗
    clsx(
    "transition-transform duration-200 w-max text-xl",
    { "scale-110": isEnabled } ,
    )
    importmeta
        7
    importmeta  
       262 天前
    antd 和 mui 都是 clsx 这种形式
    houshuu
        8
    houshuu  
       262 天前
    站第一种
    superedlimited
        9
    superedlimited  
       262 天前 via iPhone
    nextjs 教程里使用的是 clsx ,相信 vercel
    rayi
        10
    rayi  
       262 天前
    如果你的样式是使用 tailwind 这种方式添加的,那么选择第一种方式。
    如果你是使用 css-modules 或者其他手动编码 css 的话,那么选择第二种方式。
    yjw06282
        11
    yjw06282  
       262 天前
    第二种又难读, 又难看, 不知道什么有啥存在意义
    yjw06282
        12
    yjw06282  
       262 天前
    要是觉得 css 太长,. 可以提上去

    ```
    function ToggleButtonRemastered({ isEnabled, children }) {

    const css = isEnabled ? 'scale-110' : 'transition-transform duration-200 w-max text-xl'
    return (
    <button className={css}>
    {children}
    </button>
    )
    }
    ```
    okakuyang
        13
    okakuyang  
       262 天前
    说句不好听的,我看不懂.... 我都是 className={ bool ? "xxx" : "xx" },楼主这样写有什么好处吗?
    okakuyang
        14
    okakuyang  
       262 天前
    或者
    className={`xxx xx ${ bool ? "xx" : "" } ${ bool2 ? "xx": "" } `}
    karott7
        15
    karott7  
       262 天前
    第一种,可读性好太多
    qwas
        16
    qwas  
       262 天前
    之前还有的争,现在毫无疑问 tailwind
    Jtyczc
        17
    Jtyczc  
       235 天前
    肯定是第一种
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2713 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 07:41 PVG 15:41 LAX 00:41 JFK 03:41
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86