Skip to content

Commit 4f964f0

Browse files
jintoppysebmarkbage
authored andcommitted
Adding isMemo check to react-is package (#14313)
1 parent c2a2d8a commit 4f964f0

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

packages/react-is/src/ReactIs.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
REACT_ELEMENT_TYPE,
1717
REACT_FORWARD_REF_TYPE,
1818
REACT_FRAGMENT_TYPE,
19+
REACT_MEMO_TYPE,
1920
REACT_PORTAL_TYPE,
2021
REACT_PROFILER_TYPE,
2122
REACT_PROVIDER_TYPE,
@@ -27,7 +28,6 @@ import lowPriorityWarning from 'shared/lowPriorityWarning';
2728
export function typeOf(object: any) {
2829
if (typeof object === 'object' && object !== null) {
2930
const $$typeof = object.$$typeof;
30-
3131
switch ($$typeof) {
3232
case REACT_ELEMENT_TYPE:
3333
const type = object.type;
@@ -51,6 +51,7 @@ export function typeOf(object: any) {
5151
return $$typeof;
5252
}
5353
}
54+
case REACT_MEMO_TYPE:
5455
case REACT_PORTAL_TYPE:
5556
return $$typeof;
5657
}
@@ -69,6 +70,7 @@ export const ForwardRef = REACT_FORWARD_REF_TYPE;
6970
export const Fragment = REACT_FRAGMENT_TYPE;
7071
export const Profiler = REACT_PROFILER_TYPE;
7172
export const Portal = REACT_PORTAL_TYPE;
73+
export const Memo = REACT_MEMO_TYPE;
7274
export const StrictMode = REACT_STRICT_MODE_TYPE;
7375

7476
export {isValidElementType};
@@ -115,6 +117,9 @@ export function isFragment(object: any) {
115117
export function isProfiler(object: any) {
116118
return typeOf(object) === REACT_PROFILER_TYPE;
117119
}
120+
export function isMemo(object: any) {
121+
return typeOf(object) === REACT_MEMO_TYPE;
122+
}
118123
export function isPortal(object: any) {
119124
return typeOf(object) === REACT_PORTAL_TYPE;
120125
}

packages/react-is/src/__tests__/ReactIs-test.js

+8
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ describe('ReactIs', () => {
144144
expect(ReactIs.isPortal(div)).toBe(false);
145145
});
146146

147+
it('should identify memo', () => {
148+
const Component = () => React.createElement('div');
149+
const memoized = React.memo(Component);
150+
expect(ReactIs.typeOf(memoized)).toBe(ReactIs.Memo);
151+
expect(ReactIs.isMemo(memoized)).toBe(true);
152+
expect(ReactIs.isMemo(Component)).toBe(false);
153+
});
154+
147155
it('should identify strict mode', () => {
148156
expect(ReactIs.typeOf(<React.StrictMode />)).toBe(ReactIs.StrictMode);
149157
expect(ReactIs.isStrictMode(<React.StrictMode />)).toBe(true);

0 commit comments

Comments
 (0)