【问题标题】:How to Pass in Props When Using CSS Modules使用 CSS 模块时如何传入 props
【发布时间】:2020-07-05 16:20:59
【问题描述】:

我有一个按钮组件,并使用 Tailwindcss 框架和 css 模块进行额外的样式设置,如下所示。这在我使用模板文字来拉入红色背景样式时起作用。

CSS 模块:

.red {
  background-color: red;
}

.blue {
  background-color: blue;
}

反应组件:

import styles from "./style.module.css"

const Button = props => {
  return (
    <button
      className={`text-white py-2 px-4 rounded ${styles.red}`}
    >
      Button
    </button>
  )
}

但是如果我希望背景灵活并接收道具以确定背景颜色怎么办?我在想类似下面的东西,但显然不起作用:

<button
  className={`text-white py-2 px-4 rounded ${styles.`${props.bgColor}`}`}
>

【问题讨论】:

    标签: javascript reactjs css-modules


    【解决方案1】:

    事实证明,这是正确的语法:

    <button
       className={`text-white py-2 px-4 rounded ${styles[props.bgColor]}`}
    >
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-23
      • 2019-08-08
      • 2017-05-11
      • 1970-01-01
      • 2018-07-28
      • 2018-10-17
      • 1970-01-01
      • 2016-03-18
      相关资源
      最近更新 更多