Skip to content

Commit f88deda

Browse files
wherestheguacgaearon
authored andcommitted
Throw more specific error if passed undefined in React.cloneElement (#12534)
* throw error if passed undefined * should be TypeError * simplify * use invariant * editor messed up spacing * better check * Revert "better check" This reverts commit 2733707. * yarn prettier test was failing * more explicit copy * es6 import * tests * formatting * Move import
1 parent 8dfb057 commit f88deda

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

packages/react/src/ReactElement.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import invariant from 'fbjs/lib/invariant';
89
import warning from 'fbjs/lib/warning';
910
import {REACT_ELEMENT_TYPE} from 'shared/ReactSymbols';
1011

@@ -290,6 +291,12 @@ export function cloneAndReplaceKey(oldElement, newKey) {
290291
* See https://reactjs.org/docs/react-api.html#cloneelement
291292
*/
292293
export function cloneElement(element, config, children) {
294+
invariant(
295+
!(element === null || element === undefined),
296+
'React.cloneElement(...): The argument must be a React element, but you passed %s.',
297+
element,
298+
);
299+
293300
let propName;
294301

295302
// Original props are copied

packages/react/src/__tests__/ReactElementClone-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,4 +359,18 @@ describe('ReactElementClone', () => {
359359
}
360360
expect(clone.props).toEqual({foo: 'ef'});
361361
});
362+
363+
it('throws an error if passed null', () => {
364+
const element = null;
365+
expect(() => React.cloneElement(element)).toThrow(
366+
'React.cloneElement(...): The argument must be a React element, but you passed null.',
367+
);
368+
});
369+
370+
it('throws an error if passed undefined', () => {
371+
let element;
372+
expect(() => React.cloneElement(element)).toThrow(
373+
'React.cloneElement(...): The argument must be a React element, but you passed undefined.',
374+
);
375+
});
362376
});

0 commit comments

Comments
 (0)