Reactjs - 正确设置内联样式

Sim*_*ods 52 html javascript reactjs

我正在尝试将Reactjs与kendo分离器一起使用.分割器有一个样式属性

style="height: 100%"
Run Code Online (Sandbox Code Playgroud)

使用Reactjs,如果我理解正确,可以使用内联样式实现

var style = {
  height: 100
}
Run Code Online (Sandbox Code Playgroud)

但是,我也在使用Dustin Getz jsxutil来尝试将事情分成更多部分并拥有独立的html片段.到目前为止,我有以下html片段(splitter.html)

<div id="splitter" className="k-content">
  <div id="vertical">
    <div>
      <p>Outer splitter : top pane (resizable and collapsible)</p>
    </div>
    <div id="middlePane">
      {height}
      <div id="horizontal" style={height}>
        <div>
          <p>Inner splitter :: left pane</p>
        </div>
        <div>
          <p>Inner splitter :: center pane</p>
        </div>
        <div>
          <p>Inner splitter :: right pane</p>
        </div>
      </div>
    </div>
  <div>
  <p>Outer splitter : bottom pane (non-resizable, non-collapsible)</p>
</div>
Run Code Online (Sandbox Code Playgroud)

和一个splitter.js组件,它引用这个html如下

define(['react', 'external/react/js/jsxutil','text!internal/html/splitter.html'],
  function(React, jsxutil, splitterHtml) {
    'use strict';
    console.log('in app:' + splitterHtml);
    return React.createClass({

      render: function () {
        var scope = {
          height: 100
        };
        console.log('about to render:' + scope.height);

        var dom = jsxutil.exec(splitterHtml, scope);
        console.log('rendered:' + dom);
        return dom;
      }    
    });
  }
)
Run Code Online (Sandbox Code Playgroud)

现在,当我运行它时,如果我把它作为内容,我可以正确地看到高度.但是,当它作为样式属性执行时,我收到错误

The `style` prop expects a mapping from style properties to values, not a string. 
Run Code Online (Sandbox Code Playgroud)

所以我显然没有完全正确映射它.

如果有人能指导我纠正这个问题,我真的很感激.

myu*_*suf 132

您也可以尝试在style不使用变量的情况下设置内联,如下所示:

style={{"height" : "100%"}} 要么,

对于多个属性: style={{"height" : "100%", "width" : "50%"}}

  • 我无法相信文档中没有提到这一点.:) (16认同)

Goh*_*n67 32

你需要这样做:

var scope = {
     splitterStyle: {
         height: 100
     }
};
Run Code Online (Sandbox Code Playgroud)

然后将此样式应用于所需的元素:

<div id="horizontal" style={splitterStyle}>
Run Code Online (Sandbox Code Playgroud)

在您的代码中,您正在执行此操作(这是不正确的):

<div id="horizontal" style={height}>
Run Code Online (Sandbox Code Playgroud)

哪里height = 100.


小智 11

文档中可以看出为什么以下不起作用:

<span style={font-size: 1.7} class="glyphicon glyphicon-remove-sign"></span>
Run Code Online (Sandbox Code Playgroud)

但是当它完全内联时:

  • 你需要双花括号
  • 您不需要将值放在引号中
  • 如果省略,React将添加一些默认值 "em"
  • 记住在CSS中有破折号的camelCase样式名称 - 例如font-size变为fontSize:
  • classclassName

正确的方式如下:

<span style={{fontSize: 1.7 + "em"}} className="glyphicon glyphicon-remove-sign"></span>
Run Code Online (Sandbox Code Playgroud)