Reactjs - cloneWithProps重复的类名

时间:2015-04-09 05:24:01

标签: javascript node.js reactjs

我有一个手风琴反应组件,但每次更新时,它都会创建classNames的副本。

我的代码:
AccordionListComponent组件:

componentWillReceiveProps: function(nextProps) {
var accordions = this.state.accordions.map(function(accordion, i) {
  nextProps.children[i].props.key = accordion.key;

  var newAc = clone(accordion, nextProps.children[i].props);

  return React.addons.update(accordion, {
    $set: newAc
  });
});

this.setState({
  accordions: accordions
});
},

用法:

<AccordionListComponent>
        <AccordionComponent className="date" title={dateTitle} index={0}>
          <DateComponent updateDate={this.updateDate} />
        </AccordionComponent>
</AccordionListComponent>

日志

Console logs

基于ReactJs的documentation,它说明了

Do a shallow copy of element and merge any props provided by extraProps. 
The className and style props will be merged intelligently.

他们没有删除重复项吗?

感谢。

1 个答案:

答案 0 :(得分:2)

看起来像is no deduping logic

function joinClasses(className/*, ... */) {
  if (!className) {
    className = '';
  }
  var nextClass;
  var argLength = arguments.length;
  if (argLength > 1) {
    for (var ii = 1; ii < argLength; ii++) {
      nextClass = arguments[ii];
      if (nextClass) {
        className = (className ? className + ' ' : '') + nextClass;
      }
    }
  }
  return className;
}

请注意,cloneWithProps is deprecated支持新的React.cloneElement为您合并类或样式。