开发者社区 问答 正文

#React 如何有条件地应用类属性?

#React 如何有条件地应用类属性?

展开
收起
因为相信,所以看见。 2020-05-07 19:05:39 608 分享 版权
1 条回答
写回答
取消 提交回答
  • 阿里,我所有的向往

    您不应该在引号内使用大括号,因为它将被评估为字符串。

    <div className="btn-panel {this.props.visible ? 'show' : 'hidden'}">
    
    

    相反,您需要将花括号移到外面(不要忘记在类名之间包含空格):

    <div className="btn-panel {this.props.visible ? 'show' : 'hidden'}">
    
    

    模板字符串也将起作用:

    <div className={`btn-panel ${this.props.visible ? 'show' : 'hidden'}`}>
    
    2020-05-07 19:06:47
    赞同 展开评论