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

Function cloneElement

code/composition/public/app.js:19023–19083  ·  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

19021 * See https://reactjs.org/docs/react-api.html#cloneelement
19022 */
19023function cloneElement(element, config, children) {
19024 var propName = void 0;
19025
19026 // Original props are copied
19027 var props = _assign({}, element.props);
19028
19029 // Reserved names are extracted
19030 var key = element.key;
19031 var ref = element.ref;
19032 // Self is preserved since the owner is preserved.
19033 var self = element._self;
19034 // Source is preserved since cloneElement is unlikely to be targeted by a
19035 // transpiler, and the original source is probably a better indicator of the
19036 // true owner.
19037 var source = element._source;
19038
19039 // Owner will be preserved, unless ref is overridden
19040 var owner = element._owner;
19041
19042 if (config != null) {
19043 if (hasValidRef(config)) {
19044 // Silently steal the ref from the parent.
19045 ref = config.ref;
19046 owner = ReactCurrentOwner.current;
19047 }
19048 if (hasValidKey(config)) {
19049 key = '' + config.key;
19050 }
19051
19052 // Remaining properties override existing props
19053 var defaultProps = void 0;
19054 if (element.type && element.type.defaultProps) {
19055 defaultProps = element.type.defaultProps;
19056 }
19057 for (propName in config) {
19058 if (hasOwnProperty.call(config, propName) && !RESERVED_PROPS.hasOwnProperty(propName)) {
19059 if (config[propName] === undefined && defaultProps !== undefined) {
19060 // Resolve default props
19061 props[propName] = defaultProps[propName];
19062 } else {
19063 props[propName] = config[propName];
19064 }
19065 }
19066 }
19067 }
19068
19069 // Children can be more than one argument, and those are transferred onto
19070 // the newly allocated props object.
19071 var childrenLength = arguments.length - 2;
19072 if (childrenLength === 1) {
19073 props.children = children;
19074 } else if (childrenLength > 1) {
19075 var childArray = Array(childrenLength);
19076 for (var i = 0; i < childrenLength; i++) {
19077 childArray[i] = arguments[i + 2];
19078 }
19079 props.children = childArray;
19080 }

Callers

nothing calls this directly

Calls 3

hasValidRefFunction · 0.70
hasValidKeyFunction · 0.70
ReactElementFunction · 0.70

Tested by

no test coverage detected