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

Function cloneElement

code/controlled-uncontrolled/public/app.js:18209–18269  ·  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

18207 * See https://reactjs.org/docs/react-api.html#cloneelement
18208 */
18209function cloneElement(element, config, children) {
18210 var propName = void 0;
18211
18212 // Original props are copied
18213 var props = _assign({}, element.props);
18214
18215 // Reserved names are extracted
18216 var key = element.key;
18217 var ref = element.ref;
18218 // Self is preserved since the owner is preserved.
18219 var self = element._self;
18220 // Source is preserved since cloneElement is unlikely to be targeted by a
18221 // transpiler, and the original source is probably a better indicator of the
18222 // true owner.
18223 var source = element._source;
18224
18225 // Owner will be preserved, unless ref is overridden
18226 var owner = element._owner;
18227
18228 if (config != null) {
18229 if (hasValidRef(config)) {
18230 // Silently steal the ref from the parent.
18231 ref = config.ref;
18232 owner = ReactCurrentOwner.current;
18233 }
18234 if (hasValidKey(config)) {
18235 key = '' + config.key;
18236 }
18237
18238 // Remaining properties override existing props
18239 var defaultProps = void 0;
18240 if (element.type && element.type.defaultProps) {
18241 defaultProps = element.type.defaultProps;
18242 }
18243 for (propName in config) {
18244 if (hasOwnProperty.call(config, propName) && !RESERVED_PROPS.hasOwnProperty(propName)) {
18245 if (config[propName] === undefined && defaultProps !== undefined) {
18246 // Resolve default props
18247 props[propName] = defaultProps[propName];
18248 } else {
18249 props[propName] = config[propName];
18250 }
18251 }
18252 }
18253 }
18254
18255 // Children can be more than one argument, and those are transferred onto
18256 // the newly allocated props object.
18257 var childrenLength = arguments.length - 2;
18258 if (childrenLength === 1) {
18259 props.children = children;
18260 } else if (childrenLength > 1) {
18261 var childArray = Array(childrenLength);
18262 for (var i = 0; i < childrenLength; i++) {
18263 childArray[i] = arguments[i + 2];
18264 }
18265 props.children = childArray;
18266 }

Callers

nothing calls this directly

Calls 3

hasValidRefFunction · 0.70
hasValidKeyFunction · 0.70
ReactElementFunction · 0.70

Tested by

no test coverage detected