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

Function cloneElement

code/higher-order-components/public/app.js:18879–18939  ·  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

18877 * See https://reactjs.org/docs/react-api.html#cloneelement
18878 */
18879function cloneElement(element, config, children) {
18880 var propName = void 0;
18881
18882 // Original props are copied
18883 var props = _assign({}, element.props);
18884
18885 // Reserved names are extracted
18886 var key = element.key;
18887 var ref = element.ref;
18888 // Self is preserved since the owner is preserved.
18889 var self = element._self;
18890 // Source is preserved since cloneElement is unlikely to be targeted by a
18891 // transpiler, and the original source is probably a better indicator of the
18892 // true owner.
18893 var source = element._source;
18894
18895 // Owner will be preserved, unless ref is overridden
18896 var owner = element._owner;
18897
18898 if (config != null) {
18899 if (hasValidRef(config)) {
18900 // Silently steal the ref from the parent.
18901 ref = config.ref;
18902 owner = ReactCurrentOwner.current;
18903 }
18904 if (hasValidKey(config)) {
18905 key = '' + config.key;
18906 }
18907
18908 // Remaining properties override existing props
18909 var defaultProps = void 0;
18910 if (element.type && element.type.defaultProps) {
18911 defaultProps = element.type.defaultProps;
18912 }
18913 for (propName in config) {
18914 if (hasOwnProperty.call(config, propName) && !RESERVED_PROPS.hasOwnProperty(propName)) {
18915 if (config[propName] === undefined && defaultProps !== undefined) {
18916 // Resolve default props
18917 props[propName] = defaultProps[propName];
18918 } else {
18919 props[propName] = config[propName];
18920 }
18921 }
18922 }
18923 }
18924
18925 // Children can be more than one argument, and those are transferred onto
18926 // the newly allocated props object.
18927 var childrenLength = arguments.length - 2;
18928 if (childrenLength === 1) {
18929 props.children = children;
18930 } else if (childrenLength > 1) {
18931 var childArray = Array(childrenLength);
18932 for (var i = 0; i < childrenLength; i++) {
18933 childArray[i] = arguments[i + 2];
18934 }
18935 props.children = childArray;
18936 }

Callers

nothing calls this directly

Calls 3

hasValidRefFunction · 0.70
hasValidKeyFunction · 0.70
ReactElementFunction · 0.70

Tested by

no test coverage detected