MCPcopy Create free account
hub / github.com/krasimir/react-in-patterns / cloneElement

Function cloneElement

code/one-direction-data-flow/public/app.js:18758–18818  ·  view source on GitHub ↗

* Clone and return a new ReactElement using element as the starting point. * See https://reactjs.org/docs/react-api.html#cloneelement

(element, config, children)

Source from the content-addressed store, hash-verified

18756 * See https://reactjs.org/docs/react-api.html#cloneelement
18757 */
18758function cloneElement(element, config, children) {
18759 var propName = void 0;
18760
18761 // Original props are copied
18762 var props = _assign({}, element.props);
18763
18764 // Reserved names are extracted
18765 var key = element.key;
18766 var ref = element.ref;
18767 // Self is preserved since the owner is preserved.
18768 var self = element._self;
18769 // Source is preserved since cloneElement is unlikely to be targeted by a
18770 // transpiler, and the original source is probably a better indicator of the
18771 // true owner.
18772 var source = element._source;
18773
18774 // Owner will be preserved, unless ref is overridden
18775 var owner = element._owner;
18776
18777 if (config != null) {
18778 if (hasValidRef(config)) {
18779 // Silently steal the ref from the parent.
18780 ref = config.ref;
18781 owner = ReactCurrentOwner.current;
18782 }
18783 if (hasValidKey(config)) {
18784 key = '' + config.key;
18785 }
18786
18787 // Remaining properties override existing props
18788 var defaultProps = void 0;
18789 if (element.type && element.type.defaultProps) {
18790 defaultProps = element.type.defaultProps;
18791 }
18792 for (propName in config) {
18793 if (hasOwnProperty.call(config, propName) && !RESERVED_PROPS.hasOwnProperty(propName)) {
18794 if (config[propName] === undefined && defaultProps !== undefined) {
18795 // Resolve default props
18796 props[propName] = defaultProps[propName];
18797 } else {
18798 props[propName] = config[propName];
18799 }
18800 }
18801 }
18802 }
18803
18804 // Children can be more than one argument, and those are transferred onto
18805 // the newly allocated props object.
18806 var childrenLength = arguments.length - 2;
18807 if (childrenLength === 1) {
18808 props.children = children;
18809 } else if (childrenLength > 1) {
18810 var childArray = Array(childrenLength);
18811 for (var i = 0; i < childrenLength; i++) {
18812 childArray[i] = arguments[i + 2];
18813 }
18814 props.children = childArray;
18815 }

Callers

nothing calls this directly

Calls 3

hasValidRefFunction · 0.70
hasValidKeyFunction · 0.70
ReactElementFunction · 0.70

Tested by

no test coverage detected