代码之家  ›  专栏  ›  技术社区  ›  Vik

反应可重用按钮组件-我如何正确地传递背景色道具?

  •  1
  • Vik  · 技术社区  · 4 年前

    现在我正在尝试做一个可重用的按钮组件,我可以通过一个颜色道具来设置背景,这取决于它在哪个组件中。

    自定义按钮.js

    const CustomButton = ({ children, link, color }) => {
      return (
        <a href={link}>
          <button className={styles.customButton} backgroundColor={color}>
            {children}
          </button>
        </a>
      );
    };
    

    应用程序.js

    <CustomButton link='/' color='red'>Test</CustomButon>
    
    1 回复  |  直到 4 年前
        1
  •  2
  •   rwusana    4 年前

    应将背景色设置为style属性的一部分。

    const CustomButton = ({ children, link, color }) => {
      return (
        <a href={link}>
          <button 
             className={styles.customButton} 
             style={{background: color}}
          >
            {children}
          </button>
        </a>
      );
    };