【问题标题】:How to add style to React Element if it's created this way?如果以这种方式创建,如何为 React Element 添加样式?
【发布时间】:2015-05-17 00:00:22
【问题描述】:
var MyElement = React.createClass({displayName: "FormatParagraph",
    render: function () {
        return (
            React.createElement("p", null, this.props.paragraph)
            );
    }
});

如何向其中添加样式对象?

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    createElement 的第二个参数是属性的散列,其中键是属性名称,值是属性值。 style 属性将样式名称的哈希值转换为值。所以,例如:

    React.createElement("p", {style: {color: "red", backgroundColor: "blue"}}, this.props.paragraph)
    

    【讨论】:

      【解决方案2】:
      React.createElement("p", {id : 'div1', className : 'news'}, this.props.paragraph)
      

      这样你就可以在id/class里面使用App.css中指定的CSS。

      【讨论】:

        猜你喜欢
        • 2018-09-25
        • 2021-07-21
        • 2022-12-04
        • 2017-01-08
        • 2021-09-08
        • 2023-02-03
        • 2018-10-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多