Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating inline font-size style using ReactJS

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!

like image 984
kat Avatar asked Nov 05 '14 14:11

kat


People also ask

How do I change font size in Reactjs?

To change font size of text field in React Material UI, we can set the InputProps and the InputLabelProps prop to set the font size of the input box and the input label respectively. to set InputProps and InputLabelProps to { style: { fontSize: 40 } } so that the input box and the label both have font size 40px.

How do I change the font size on inline?

To change the size of your text with inline CSS, you have to do it with the style attribute. You type in the font-size property, and then assign it a value.

Can We Use inline CSS in React?

In React, inline styles are not specified as a string. The style attribute accepts a JavaScript object with camelCased properties. Below are the basic steps for defining inline CSS: 1. Change the CSS property name to its camelCase version like "background-color" to "backgroundColor", "font-size" to "fontSize", etc.


2 Answers

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 :)

like image 167
kat Avatar answered Sep 30 '22 00:09

kat


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!

like image 25
S.Yadav Avatar answered Sep 29 '22 23:09

S.Yadav