MCPcopy
hub / github.com/facebook/react / cloneElement

Function cloneElement

packages/react/src/jsx/ReactJSXElement.js:708–788  ·  view source on GitHub ↗
(element, config, children)

Source from the content-addressed store, hash-verified

706 * See https://reactjs.org/docs/react-api.html#cloneelement
707 */
708export function cloneElement(element, config, children) {
709 if (element === null || element === undefined) {
710 throw new Error(
711 `The argument must be a React element, but you passed ${element}.`,
712 );
713 }
714
715 let propName;
716
717 // Original props are copied
718 const props = assign({}, element.props);
719
720 // Reserved names are extracted
721 let key = element.key;
722
723 // Owner will be preserved, unless ref is overridden
724 let owner = !__DEV__ ? undefined : element._owner;
725
726 if (config != null) {
727 if (hasValidRef(config)) {
728 owner = __DEV__ ? getOwner() : undefined;
729 }
730 if (hasValidKey(config)) {
731 if (__DEV__) {
732 checkKeyStringCoercion(config.key);
733 }
734 key = '' + config.key;
735 }
736
737 // Remaining properties override existing props
738 for (propName in config) {
739 if (
740 hasOwnProperty.call(config, propName) &&
741 // Skip over reserved prop names
742 propName !== 'key' &&
743 // ...and maybe these, too, though we currently rely on them for
744 // warnings and debug information in dev. Need to decide if we're OK
745 // with dropping them. In the jsx() runtime it's not an issue because
746 // the data gets passed as separate arguments instead of props, but
747 // it would be nice to stop relying on them entirely so we can drop
748 // them from the internal Fiber field.
749 propName !== '__self' &&
750 propName !== '__source' &&
751 // Undefined `ref` is ignored by cloneElement. We treat it the same as
752 // if the property were missing. This is mostly for
753 // backwards compatibility.
754 !(propName === 'ref' && config.ref === undefined)
755 ) {
756 props[propName] = config[propName];
757 }
758 }
759 }
760
761 // Children can be more than one argument, and those are transferred onto
762 // the newly allocated props object.
763 const childrenLength = arguments.length - 2;
764 if (childrenLength === 1) {
765 props.children = children;

Callers

nothing calls this directly

Calls 6

checkKeyStringCoercionFunction · 0.90
hasValidRefFunction · 0.70
getOwnerFunction · 0.70
hasValidKeyFunction · 0.70
ReactElementFunction · 0.70
validateChildKeysFunction · 0.70

Tested by

no test coverage detected