Skip to content

Commit

Permalink
Adding isMemo check to react-is package (#14313)
Browse files Browse the repository at this point in the history
  • Loading branch information
jintoppy authored and sebmarkbage committed Nov 28, 2018
1 parent c2a2d8a commit 4f964f0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/react-is/src/ReactIs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
REACT_ELEMENT_TYPE,
REACT_FORWARD_REF_TYPE,
REACT_FRAGMENT_TYPE,
REACT_MEMO_TYPE,
REACT_PORTAL_TYPE,
REACT_PROFILER_TYPE,
REACT_PROVIDER_TYPE,
Expand All @@ -27,7 +28,6 @@ import lowPriorityWarning from 'shared/lowPriorityWarning';
export function typeOf(object: any) {
if (typeof object === 'object' && object !== null) {
const $$typeof = object.$$typeof;

switch ($$typeof) {
case REACT_ELEMENT_TYPE:
const type = object.type;
Expand All @@ -51,6 +51,7 @@ export function typeOf(object: any) {
return $$typeof;
}
}
case REACT_MEMO_TYPE:
case REACT_PORTAL_TYPE:
return $$typeof;
}
Expand All @@ -69,6 +70,7 @@ export const ForwardRef = REACT_FORWARD_REF_TYPE;
export const Fragment = REACT_FRAGMENT_TYPE;
export const Profiler = REACT_PROFILER_TYPE;
export const Portal = REACT_PORTAL_TYPE;
export const Memo = REACT_MEMO_TYPE;
export const StrictMode = REACT_STRICT_MODE_TYPE;

export {isValidElementType};
Expand Down Expand Up @@ -115,6 +117,9 @@ export function isFragment(object: any) {
export function isProfiler(object: any) {
return typeOf(object) === REACT_PROFILER_TYPE;
}
export function isMemo(object: any) {
return typeOf(object) === REACT_MEMO_TYPE;
}
export function isPortal(object: any) {
return typeOf(object) === REACT_PORTAL_TYPE;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/react-is/src/__tests__/ReactIs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ describe('ReactIs', () => {
expect(ReactIs.isPortal(div)).toBe(false);
});

it('should identify memo', () => {
const Component = () => React.createElement('div');
const memoized = React.memo(Component);
expect(ReactIs.typeOf(memoized)).toBe(ReactIs.Memo);
expect(ReactIs.isMemo(memoized)).toBe(true);
expect(ReactIs.isMemo(Component)).toBe(false);
});

it('should identify strict mode', () => {
expect(ReactIs.typeOf(<React.StrictMode />)).toBe(ReactIs.StrictMode);
expect(ReactIs.isStrictMode(<React.StrictMode />)).toBe(true);
Expand Down

0 comments on commit 4f964f0

Please sign in to comment.