javascript - 如何创建接受颜色作为参数的 React 组件?

标签 javascript css reactjs

假设我想使用 React 创建一个自定义按钮。我想让这个组件接受颜色作为它的属性,并根据属性中的颜色呈现自己。

class MyButton extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    const { name, color } = this.props;

    return (
      <div className="my-button">
        <button type="button" className="btn">
          {name}
        </button>
      </div>
    );
  }
}

如何将 color 注入(inject)按钮 css 样式(边框和字体颜色)?

最佳答案

使用类似的东西:

编辑:也添加了边框颜色代码

class MyButton extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    const { name, color } = this.props;

    return (
      <div className="my-button">
        <button type="button" className="btn" 
          style={{
             color,
             borderColor:color
                 }}>
          {name}
        </button>
      </div>
    );
  }
}

关于javascript - 如何创建接受颜色作为参数的 React 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48957798/

相关文章:

javascript - 使用 Bootstrap 的画廊选择器不起作用

javascript - 尝试通过调用使用 axios 的函数(异步)来填充数组,并且该函数在填充数组之前返回未定义

javascript - 使用 ngMessages 验证自定义指令

javascript - jQuery Flot - 四舍五入 x 轴值?

javascript - jQuery Mobile Ajax(js不执行)

javascript - 以 Angular 制作可编辑网格的最佳实践

html - IE 11 无法正确显示 CSS 填充

javascript - 在 css/html 响应式中创建菜单栏

javascript - this.textInput.focus 不是函数 - React

javascript - 传单弹出窗口不会打开( react )