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

Function cloneElement

code/third-party/public/app.js:18118–18178  ·  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

18116 * See https://reactjs.org/docs/react-api.html#cloneelement
18117 */
18118function cloneElement(element, config, children) {
18119 var propName = void 0;
18120
18121 // Original props are copied
18122 var props = _assign({}, element.props);
18123
18124 // Reserved names are extracted
18125 var key = element.key;
18126 var ref = element.ref;
18127 // Self is preserved since the owner is preserved.
18128 var self = element._self;
18129 // Source is preserved since cloneElement is unlikely to be targeted by a
18130 // transpiler, and the original source is probably a better indicator of the
18131 // true owner.
18132 var source = element._source;
18133
18134 // Owner will be preserved, unless ref is overridden
18135 var owner = element._owner;
18136
18137 if (config != null) {
18138 if (hasValidRef(config)) {
18139 // Silently steal the ref from the parent.
18140 ref = config.ref;
18141 owner = ReactCurrentOwner.current;
18142 }
18143 if (hasValidKey(config)) {
18144 key = '' + config.key;
18145 }
18146
18147 // Remaining properties override existing props
18148 var defaultProps = void 0;
18149 if (element.type && element.type.defaultProps) {
18150 defaultProps = element.type.defaultProps;
18151 }
18152 for (propName in config) {
18153 if (hasOwnProperty.call(config, propName) && !RESERVED_PROPS.hasOwnProperty(propName)) {
18154 if (config[propName] === undefined && defaultProps !== undefined) {
18155 // Resolve default props
18156 props[propName] = defaultProps[propName];
18157 } else {
18158 props[propName] = config[propName];
18159 }
18160 }
18161 }
18162 }
18163
18164 // Children can be more than one argument, and those are transferred onto
18165 // the newly allocated props object.
18166 var childrenLength = arguments.length - 2;
18167 if (childrenLength === 1) {
18168 props.children = children;
18169 } else if (childrenLength > 1) {
18170 var childArray = Array(childrenLength);
18171 for (var i = 0; i < childrenLength; i++) {
18172 childArray[i] = arguments[i + 2];
18173 }
18174 props.children = childArray;
18175 }

Callers

nothing calls this directly

Calls 3

hasValidRefFunction · 0.70
hasValidKeyFunction · 0.70
ReactElementFunction · 0.70

Tested by

no test coverage detected