Home » Reactjs » How to add style to React Element if it's created this way?

How to add style to React Element if it's created this way?

Posted by: admin January 30, 2018 Leave a comment

Questions:
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?

Answers:

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)