(element, newRef)
| 2 | import { cloneElement } from 'react'; |
| 3 | |
| 4 | export 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 | } |
no outgoing calls
no test coverage detected