Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 576
PythonIsGreat Avatar asked May 17 '15 00:05

PythonIsGreat


People also ask

Can we add style to React fragment?

If you need to style a Fragment then you probably shouldn't be using it in the first place. Fragments exists as a workaround to return adjacent JSX elements, they don't render anything to the DOM so they can't be stylized.

How do I add a component style?

There are several ways to add styles to a component: By setting styles or styleUrls metadata. Inline in the template HTML. With CSS imports.

How do you give a style prop in React?

Use the React. CSSProperties type to pass CSS styles as props in React TypeScript, e.g. style: React. CSSProperties; . The CSSProperties type is used to type the style object that consists of CSS property names and values.


2 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)
like image 121
Michelle Tilley Avatar answered Oct 03 '22 19:10

Michelle Tilley


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.

like image 29
Shweta D'Sa Avatar answered Oct 03 '22 21:10

Shweta D'Sa