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

Function cloneElement

code/styling/public/app.js:18885–18945  ·  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

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

Callers

nothing calls this directly

Calls 3

hasValidRefFunction · 0.70
hasValidKeyFunction · 0.70
ReactElementFunction · 0.70

Tested by

no test coverage detected