PythonIsGreat
PythonIsGreat

Reputation: 7907

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

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?

Upvotes: 25

Views: 33360

Answers (2)

Shweta D'Sa
Shweta D'Sa

Reputation: 71

React.createElement("p", {id : 'div1', className : 'news'}, this.props.paragraph)

This way you can use CSS specified in App.css inside in an id/class.

Upvotes: 4

Michelle Tilley
Michelle Tilley

Reputation: 159105

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)

Upvotes: 51

Related Questions