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 273370758e.

* yarn prettier test was failing

* more explicit copy

* es6 import

* tests

* formatting

* Move import
This commit is contained in:
Nicole Levy
2018-04-10 02:16:36 +01:00
committed by Dan Abramov
parent 8dfb057881
commit f88deda83b
2 changed files with 21 additions and 0 deletions
+7
View File
@@ -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';
@@ -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
@@ -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.',
);
});
});