-
Notifications
You must be signed in to change notification settings - Fork 47k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add React.isValidElementType() (#12483)
* Add React.isValidElementType() Per the conversation on #12453, there are a number of third-party libraries (particularly those that generate higher-order components) that are performing suboptimal validation of element types. This commit exposes a function that can perform the desired check without depending upon React internals. * Move isValidElementType to shared/
- Loading branch information
1 parent
125dd16
commit 96fe3b1
Showing
4 changed files
with
75 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright (c) 2016-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import { | ||
REACT_FRAGMENT_TYPE, | ||
REACT_ASYNC_MODE_TYPE, | ||
REACT_STRICT_MODE_TYPE, | ||
REACT_PROVIDER_TYPE, | ||
REACT_CONTEXT_TYPE, | ||
REACT_FORWARD_REF_TYPE, | ||
} from 'shared/ReactSymbols'; | ||
|
||
export default function isValidElementType(type: mixed) { | ||
return ( | ||
typeof type === 'string' || | ||
typeof type === 'function' || | ||
// Note: its typeof might be other than 'symbol' or 'number' if it's a polyfill. | ||
type === REACT_FRAGMENT_TYPE || | ||
type === REACT_ASYNC_MODE_TYPE || | ||
type === REACT_STRICT_MODE_TYPE || | ||
(typeof type === 'object' && | ||
type !== null && | ||
(type.$$typeof === REACT_PROVIDER_TYPE || | ||
type.$$typeof === REACT_CONTEXT_TYPE || | ||
type.$$typeof === REACT_FORWARD_REF_TYPE)) | ||
); | ||
} |