【问题标题】:extending JSX css element扩展 JSX CSS 元素
【发布时间】:2019-06-20 14:33:13
【问题描述】:

在 react JSX 主题中,我有 2 个 CSS 元素。

button1: {
        color: 'white',
        borderColor: 'green',
        backgroundColor: 'white',
        "&:hover": {
            backgroundColor:"white"
        },
        borderTopRightRadius: "18px",
        borderBottomRightRadius: "18px",
        borderTopLeftRadius: "18px",
        borderBottomLeftRadius: "18px",
        width: '180px'
}
button2: {
        color: '#7F4095',
        borderColor: '#7F4095',
        backgroundColor: 'white',
        "&:hover": {
            backgroundColor:"white"
        },      
        borderTopRightRadius: "18px",
        borderBottomRightRadius: "18px",
        borderTopLeftRadius: "18px",
        borderBottomLeftRadius: "18px",
        width: '180px'
}

如果您看到大多数属性是相同的。我如何确保我有 2 个元素,但它们重用了代码。

【问题讨论】:

    标签: css reactjs jsx


    【解决方案1】:

    您可以使用展开语法:

    const button_proto = {
      backgroundColor: 'white',
      "&:hover": {
        backgroundColor: "white"
      },
      borderTopRightRadius: "18px",
      borderBottomRightRadius: "18px",
      borderTopLeftRadius: "18px",
      borderBottomLeftRadius: "18px",
      width: '180px'
    }
    
    const styles = {
      button1: {
        ...button_proto,
        color: 'white',
        borderColor: 'green'
      },
      button2: {
        ...button_proto,
        color: '#7F4095',
        borderColor: '#7F4095',
      }
    }
    

    Documentation for spread syntax (...)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-25
      • 2018-07-05
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      相关资源
      最近更新 更多