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

Function cloneElement

code/new-context-api/public/app.js:18246–18306  ·  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

18244 * See https://reactjs.org/docs/react-api.html#cloneelement
18245 */
18246function cloneElement(element, config, children) {
18247 var propName = void 0;
18248
18249 // Original props are copied
18250 var props = _assign({}, element.props);
18251
18252 // Reserved names are extracted
18253 var key = element.key;
18254 var ref = element.ref;
18255 // Self is preserved since the owner is preserved.
18256 var self = element._self;
18257 // Source is preserved since cloneElement is unlikely to be targeted by a
18258 // transpiler, and the original source is probably a better indicator of the
18259 // true owner.
18260 var source = element._source;
18261
18262 // Owner will be preserved, unless ref is overridden
18263 var owner = element._owner;
18264
18265 if (config != null) {
18266 if (hasValidRef(config)) {
18267 // Silently steal the ref from the parent.
18268 ref = config.ref;
18269 owner = ReactCurrentOwner.current;
18270 }
18271 if (hasValidKey(config)) {
18272 key = '' + config.key;
18273 }
18274
18275 // Remaining properties override existing props
18276 var defaultProps = void 0;
18277 if (element.type && element.type.defaultProps) {
18278 defaultProps = element.type.defaultProps;
18279 }
18280 for (propName in config) {
18281 if (hasOwnProperty.call(config, propName) && !RESERVED_PROPS.hasOwnProperty(propName)) {
18282 if (config[propName] === undefined && defaultProps !== undefined) {
18283 // Resolve default props
18284 props[propName] = defaultProps[propName];
18285 } else {
18286 props[propName] = config[propName];
18287 }
18288 }
18289 }
18290 }
18291
18292 // Children can be more than one argument, and those are transferred onto
18293 // the newly allocated props object.
18294 var childrenLength = arguments.length - 2;
18295 if (childrenLength === 1) {
18296 props.children = children;
18297 } else if (childrenLength > 1) {
18298 var childArray = Array(childrenLength);
18299 for (var i = 0; i < childrenLength; i++) {
18300 childArray[i] = arguments[i + 2];
18301 }
18302 props.children = childArray;
18303 }

Callers

nothing calls this directly

Calls 3

hasValidRefFunction · 0.70
hasValidKeyFunction · 0.70
ReactElementFunction · 0.70

Tested by

no test coverage detected