Skip to content

Commit

Permalink
Throw more specific error if passed undefined in React.cloneElement (f…
Browse files Browse the repository at this point in the history
…acebook#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
  • Loading branch information
wherestheguac authored and rhagigi committed Apr 19, 2018
1 parent d6bef36 commit ce45bf6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/react/src/ReactElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import invariant from 'fbjs/lib/invariant';
import warning from 'fbjs/lib/warning';
import {REACT_ELEMENT_TYPE} from 'shared/ReactSymbols';

Expand Down Expand Up @@ -290,6 +291,12 @@ export function cloneAndReplaceKey(oldElement, newKey) {
* See https://reactjs.org/docs/react-api.html#cloneelement
*/
export function cloneElement(element, config, children) {
invariant(
!(element === null || element === undefined),
'React.cloneElement(...): The argument must be a React element, but you passed %s.',
element,
);

let propName;

// Original props are copied
Expand Down
14 changes: 14 additions & 0 deletions packages/react/src/__tests__/ReactElementClone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,18 @@ describe('ReactElementClone', () => {
}
expect(clone.props).toEqual({foo: 'ef'});
});

it('throws an error if passed null', () => {
const element = null;
expect(() => React.cloneElement(element)).toThrow(
'React.cloneElement(...): The argument must be a React element, but you passed null.',
);
});

it('throws an error if passed undefined', () => {
let element;
expect(() => React.cloneElement(element)).toThrow(
'React.cloneElement(...): The argument must be a React element, but you passed undefined.',
);
});
});

0 comments on commit ce45bf6

Please sign in to comment.