id: clone-with-props title: 克隆组件 permalink: clone-with-props.html prev: test-utils.html

next: update.html

在极少数应用场景中,一个组件可能想改变另一个它不拥有的组件的props(就像改变一个组件的className,这个组件又作为this.props.children传入)。其它的时候,可能想生成传进来的一个组件的多个拷贝。cloneWithProps()使其成为可能。

ReactComponent React.addons.cloneWithProps(ReactComponent component, object? extraProps)

做一个component的浅复制,合并extraProps提供的每一个props。classNamestyle props将会被智能合并。

注意:

cloneWithProps并不传递key到克隆的组件中。如果你希望保留key,将其添加到extraProps对象:

var clonedComponent = cloneWithProps(originalComponent, { key : originalComponent.key });

ref也一样不会保留。