如果以这种方式创建 React Element,如何向它添加样式? [英] How to add style to React Element if it's created this way?

查看:34
本文介绍了如果以这种方式创建 React Element,如何向它添加样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var MyElement = React.createClass({displayName: "FormatParagraph",
    render: function () {
        return (
            React.createElement("p", null, this.props.paragraph)
            );
    }
});

如何向其添加样式对象?

How can I add a style object to this?

推荐答案

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

The second parameter to createElement is a hash of attributes where the key is the attribute name and the value is the attribute value. The style attribute takes a hash of style names to values. So, for example:

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

这篇关于如果以这种方式创建 React Element,如何向它添加样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆