Cloning ReactElements

Note:cloneWithProps is deprecated. Use React.cloneElement instead.

In rare situations, you may want to create a copy of a React element with different props from those of the original element. One example is cloning the elements passed into this.props.children and rendering them with different props:

  1. var cloneWithProps = require('react-addons-clone-with-props');
  2. var _makeBlue = function(element) {
  3. return cloneWithProps(element, {style: {color: 'blue'}});
  4. };
  5. var Blue = React.createClass({
  6. render: function() {
  7. var blueChildren = React.Children.map(this.props.children, _makeBlue);
  8. return <div>{blueChildren}</div>;
  9. }
  10. });
  11. ReactDOM.render(
  12. <Blue>
  13. <p>This text is blue.</p>
  14. </Blue>,
  15. document.getElementById('container')
  16. );

cloneWithProps does not transfer key or ref to the cloned element. className and style props are automatically merged.