Generating inline font-size style using ReactJS

114,329

Solution 1

Use fontSize instead of font-size

the solution is to camelCase properties which usually contain a dash

http://facebook.github.io/react/tips/inline-styles.html

Answered my own question :)

Solution 2

I use fontSize: pixels numbers

Solution 3

As https://reactjs.org/docs/dom-elements.html says,
We need to remove '-' and upperCase except first word

Example-
background-color as backgroundColor,

Same will be applicable everywhere except a few as-

aria-* and data-*

example-

aria-label as aria-label 

Above worked for me!

Share:
114,329

Related videos on Youtube

kat
Author by

kat

Updated on July 09, 2022

Comments

  • kat
    kat almost 2 years

    I am trying to do something like this in ReactJS:

    var MyReactClass = React.createClass({
        render: function() {
            var myDivText = "Hello!";
            var myFontSize = 6; //this is actually something more complicated, I'm calculating it on the fly
            var divStyle = {
                font-size: {fontSize + 'px !important;'},
            };
            return (<div style={divStyle}>{myDivText}</div>);
       }
    });
    

    The problem is that I get this error when I run my code: "Module build failed: Error: Parse Error: Line 5: Unexpected token -" apparently, React doesn't like that font-size has a dash in it. How do I get around this? Is there some way to escape that character for react? Is there some different css property that react likes better that does the same thing?

    Thanks!

    • David Hellsing
      David Hellsing over 9 years
      It’s actually JavaScript that throws the error first. An unquoted property name must be a valid identifier name or numeric literal. Valid code would be: {'font-size': '10px'} (with quotes). Allthough I’m not sure how React would handle it.
    • kat
      kat over 9 years
      It's true that JavaScript has those restrictions on unquoted property names, but this is actually a key in a React style object, not a javascript property, if I understand correctly
    • Jake Acosta
      Jake Acosta almost 6 years
      camelCase works inline, and non-camel stylings work in stylesheets. Stylesheets are a common best practice so others don't have to worry about missing anything in a dense code block. Glad you figured it out though!
  • Andy B
    Andy B over 9 years
    And if you have something like: -webkit-box-shadow, then just put webkitBoxShadow
  • bradcush
    bradcush almost 9 years
    @AndyB You need to actually start that with a capital W so it would be "WebkitBoxShadow". React states vendor prefixes other than ms start with a capital. facebook.github.io/react/tips/inline-styles.html
  • Arye Eidelman
    Arye Eidelman almost 5 years
    You are confusing HTML attributes like aria-label and data-*, [casing-docs]( reactjs.org/docs/dom-elements.html) and CSS properties created with inline styles like backgroundColor and WebkitTransition reactjs.org/docs/dom-elements.html#style
  • S.Yadav
    S.Yadav almost 5 years
    There is nothing to get confuse, instead-of background-color need to use backgroundColor. It is so simple.