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

Function cloneElement

code/communication/public/app.js:18920–18980  ·  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

18918 * See https://reactjs.org/docs/react-api.html#cloneelement
18919 */
18920function cloneElement(element, config, children) {
18921 var propName = void 0;
18922
18923 // Original props are copied
18924 var props = _assign({}, element.props);
18925
18926 // Reserved names are extracted
18927 var key = element.key;
18928 var ref = element.ref;
18929 // Self is preserved since the owner is preserved.
18930 var self = element._self;
18931 // Source is preserved since cloneElement is unlikely to be targeted by a
18932 // transpiler, and the original source is probably a better indicator of the
18933 // true owner.
18934 var source = element._source;
18935
18936 // Owner will be preserved, unless ref is overridden
18937 var owner = element._owner;
18938
18939 if (config != null) {
18940 if (hasValidRef(config)) {
18941 // Silently steal the ref from the parent.
18942 ref = config.ref;
18943 owner = ReactCurrentOwner.current;
18944 }
18945 if (hasValidKey(config)) {
18946 key = '' + config.key;
18947 }
18948
18949 // Remaining properties override existing props
18950 var defaultProps = void 0;
18951 if (element.type && element.type.defaultProps) {
18952 defaultProps = element.type.defaultProps;
18953 }
18954 for (propName in config) {
18955 if (hasOwnProperty.call(config, propName) && !RESERVED_PROPS.hasOwnProperty(propName)) {
18956 if (config[propName] === undefined && defaultProps !== undefined) {
18957 // Resolve default props
18958 props[propName] = defaultProps[propName];
18959 } else {
18960 props[propName] = config[propName];
18961 }
18962 }
18963 }
18964 }
18965
18966 // Children can be more than one argument, and those are transferred onto
18967 // the newly allocated props object.
18968 var childrenLength = arguments.length - 2;
18969 if (childrenLength === 1) {
18970 props.children = children;
18971 } else if (childrenLength > 1) {
18972 var childArray = Array(childrenLength);
18973 for (var i = 0; i < childrenLength; i++) {
18974 childArray[i] = arguments[i + 2];
18975 }
18976 props.children = childArray;
18977 }

Callers

nothing calls this directly

Calls 3

hasValidRefFunction · 0.70
hasValidKeyFunction · 0.70
ReactElementFunction · 0.70

Tested by

no test coverage detected