使用ReactJS生成内联字体大小样式

kat*_*kat 23 css font-size inline-styles reactjs react-jsx

我想在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>);
   }
});
Run Code Online (Sandbox Code Playgroud)

问题是当我运行我的代码时出现这个错误:"模块构建失败:错误:解析错误:第5行:意外的令牌 - "显然,React不喜欢它font-size有一个破折号.我该如何解决这个问题?有没有办法逃脱这个角色的反应?是否有一些不同的css属性反应更好,做同样的事情?

谢谢!

kat*_*kat 51

fontSize而不是font-size

解决方案是camelCase属性,通常包含破折号

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

回答了我自己的问题:)

  • @AndyB你需要用大写的W开始,所以它将是"WebkitBoxShadow".React声明除ms之外的供应商前缀以大写字母开头.https://facebook.github.io/react/tips/inline-styles.html (7认同)