MCPcopy Create free account
hub / github.com/react-dnd/react-dnd / cloneWithRef

Function cloneWithRef

packages/react-dnd/src/utils/cloneWithRef.js:4–29  ·  view source on GitHub ↗
(element, newRef)

Source from the content-addressed store, hash-verified

2import { cloneElement } from 'react';
3
4export default function cloneWithRef(element, newRef) {
5 const previousRef = element.ref;
6 invariant(
7 typeof previousRef !== 'string',
8 'Cannot connect React DnD to an element with an existing string ref. ' +
9 'Please convert it to use a callback ref instead, or wrap it into a <span> or <div>. ' +
10 'Read more: https://facebook.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute',
11 );
12
13 if (!previousRef) {
14 // When there is no ref on the element, use the new ref directly
15 return cloneElement(element, {
16 ref: newRef,
17 });
18 }
19
20 return cloneElement(element, {
21 ref: (node) => {
22 newRef(node);
23
24 if (previousRef) {
25 previousRef(node);
26 }
27 },
28 });
29}

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected