Home / Function/ cloneElement() — react Function Reference

cloneElement() — react Function Reference

Architecture documentation for the cloneElement() function in ReactJSXElement.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  e25f8b64_9c1b_41a7_d00a_3663a86ab332["cloneElement()"]
  1bf5591f_27a1_c79f_853a_6242549e0e07["ReactJSXElement.js"]
  e25f8b64_9c1b_41a7_d00a_3663a86ab332 -->|defined in| 1bf5591f_27a1_c79f_853a_6242549e0e07
  586a9a31_9293_5e79_1a20_3d378d66778a["hasValidRef()"]
  e25f8b64_9c1b_41a7_d00a_3663a86ab332 -->|calls| 586a9a31_9293_5e79_1a20_3d378d66778a
  5c6da8c1_f2fa_cf23_63c2_fe9e5615d850["getOwner()"]
  e25f8b64_9c1b_41a7_d00a_3663a86ab332 -->|calls| 5c6da8c1_f2fa_cf23_63c2_fe9e5615d850
  04bd1671_ef17_1a3e_4550_297a31a727d2["hasValidKey()"]
  e25f8b64_9c1b_41a7_d00a_3663a86ab332 -->|calls| 04bd1671_ef17_1a3e_4550_297a31a727d2
  9e375f3a_dd2f_18ab_e2b2_0d6efc63b4b6["ReactElement()"]
  e25f8b64_9c1b_41a7_d00a_3663a86ab332 -->|calls| 9e375f3a_dd2f_18ab_e2b2_0d6efc63b4b6
  79da1863_e9f9_d1a8_fbf7_51287452d2c5["validateChildKeys()"]
  e25f8b64_9c1b_41a7_d00a_3663a86ab332 -->|calls| 79da1863_e9f9_d1a8_fbf7_51287452d2c5
  style e25f8b64_9c1b_41a7_d00a_3663a86ab332 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/react/src/jsx/ReactJSXElement.js lines 770–854

export function cloneElement(element, config, children) {
  if (element === null || element === undefined) {
    throw new Error(
      `The argument must be a React element, but you passed ${element}.`,
    );
  }

  let propName;

  // Original props are copied
  const props = assign({}, element.props);

  // Reserved names are extracted
  let key = element.key;

  // Owner will be preserved, unless ref is overridden
  let owner = !__DEV__ ? undefined : element._owner;

  if (config != null) {
    if (hasValidRef(config)) {
      owner = __DEV__ ? getOwner() : undefined;
    }
    if (hasValidKey(config)) {
      if (enableOptimisticKey && config.key === REACT_OPTIMISTIC_KEY) {
        key = REACT_OPTIMISTIC_KEY;
      } else {
        if (__DEV__) {
          checkKeyStringCoercion(config.key);
        }
        key = '' + config.key;
      }
    }

    // Remaining properties override existing props
    for (propName in config) {
      if (
        hasOwnProperty.call(config, propName) &&
        // Skip over reserved prop names
        propName !== 'key' &&
        // ...and maybe these, too, though we currently rely on them for
        // warnings and debug information in dev. Need to decide if we're OK
        // with dropping them. In the jsx() runtime it's not an issue because
        // the data gets passed as separate arguments instead of props, but
        // it would be nice to stop relying on them entirely so we can drop
        // them from the internal Fiber field.
        propName !== '__self' &&
        propName !== '__source' &&
        // Undefined `ref` is ignored by cloneElement. We treat it the same as
        // if the property were missing. This is mostly for
        // backwards compatibility.
        !(propName === 'ref' && config.ref === undefined)
      ) {
        props[propName] = config[propName];
      }
    }
  }

  // Children can be more than one argument, and those are transferred onto
  // the newly allocated props object.
  const childrenLength = arguments.length - 2;
  if (childrenLength === 1) {
    props.children = children;
  } else if (childrenLength > 1) {
    const childArray = Array(childrenLength);
    for (let i = 0; i < childrenLength; i++) {
      childArray[i] = arguments[i + 2];
    }
    props.children = childArray;
  }

  const clonedElement = ReactElement(
    element.type,
    key,
    props,
    owner,
    __DEV__ && element._debugStack,
    __DEV__ && element._debugTask,
  );

  for (let i = 2; i < arguments.length; i++) {
    validateChildKeys(arguments[i]);

Domain

Subdomains

Frequently Asked Questions

What does cloneElement() do?
cloneElement() is a function in the react codebase, defined in packages/react/src/jsx/ReactJSXElement.js.
Where is cloneElement() defined?
cloneElement() is defined in packages/react/src/jsx/ReactJSXElement.js at line 770.
What does cloneElement() call?
cloneElement() calls 5 function(s): ReactElement, getOwner, hasValidKey, hasValidRef, validateChildKeys.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free