ReactFix
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • React
  • React Native
  • Programming
  • Object Oriented Programming

Saturday, November 26, 2022

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

 November 26, 2022     reactjs   

Issue

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?


Solution

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)


Answered By - Michelle Tilley
Answer Checked By - - Willingham (ReactFix Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post Home

Featured Post

Is Learning React Difficult?

React is difficult to learn, no ifs and buts. React isn't inherently difficult to learn. It's a framework that's different from ...

Total Pageviews

Copyright © ReactFix