在極少數(shù)應用場景中,一個組件可能想改變另一個它不擁有的組件的props(就像改變一個組件的className,這個組件又作為this.props.children傳入)。其它的時候,可能想生成傳進來的一個組件的多個拷貝。cloneWithProps()使其成為可能。
ReactComponent React.addons.cloneWithProps(ReactComponent component, object? extraProps)做一個component的淺復制,合并extraProps提供的每一個props。className和style props將會被智能合并。
注意:
cloneWithProps并不傳遞key到克隆的組件中。如果你希望保留key,將其添加到extraProps對象:var clonedComponent = cloneWithProps(originalComponent, { key : originalComponent.key });
ref也一樣不會保留。