ReactJS - 将样式作为动态对象传递

时间:2015-12-29 22:43:00

标签: reactjs

我想创建一个可重复使用的选择控件:

const selectProps = this.props.selectProps;
const label = selectProps.label;

 ....

  <div className={ selectProps.divStyle }>
        <label htmlFor={ selectProps.selectName } style={ label.style }> <-- not working
          { label.title }
        </label>
        <select name={ selectProps.selectName }
                value={ this.state.selectedValue }
                onChange={this.change.bind(this)}
        >
          { this.state.options }
        </select>
      </div>

从这个JSON对象:

const selectControl = {
  "label": {
    "title": "items per page",
    "style" : "display: 'none'"
  },
  "divStyle" : "four columns",
  "selectName" : "ItemsPerPageSelect",
  "options": [
  {"text" : "Select items per page", "value" : null, "isSelected": true},
  {"text" : "15 items per page", "value" : "15"},
  {"text" : "30 items per page", "value" : "30"},
  {"text" : "60 items per page", "value" : "60"},
  {"text" : "90 items per page", "value" : "90"},
  {"text" : "120 items per page", "value" : "120"}
]
};

标签样式不起作用。

格式化可以作为样式属性传递给元素的json对象的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

没关系......

  "label": {
    "title": "items per page",
    "style" : {
      "display" : "none"
    }
  },