mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
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:
@@ -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.',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user