@samitouri / QOS-React / commits / 4f964f09c1

Adding isMemo check to react-is package (#14313)

Jinto Jose committed Nov 29, 2018 at 02:21 UTC 4f964f09c12e531ac44d48127782e811ad8d75a7
2 files changed +14 -1
packages/react-is/src/ReactIs.js
+6 -1
@@ -16,6 +16,7 @@ import {
16 REACT_ELEMENT_TYPE,
17 REACT_FORWARD_REF_TYPE,
18 REACT_FRAGMENT_TYPE,
19 + REACT_MEMO_TYPE,
20 REACT_PORTAL_TYPE,
21 REACT_PROFILER_TYPE,
22 REACT_PROVIDER_TYPE,
@@ -27,7 +28,6 @@ import lowPriorityWarning from 'shared/lowPriorityWarning';
28 export function typeOf(object: any) {
29 if (typeof object === 'object' && object !== null) {
30 const $$typeof = object.$$typeof;
30 -
31 switch ($$typeof) {
32 case REACT_ELEMENT_TYPE:
33 const type = object.type;
@@ -51,6 +51,7 @@ export function typeOf(object: any) {
51 return $$typeof;
52 }
53 }
54 + case REACT_MEMO_TYPE:
55 case REACT_PORTAL_TYPE:
56 return $$typeof;
57 }
@@ -69,6 +70,7 @@ export const ForwardRef = REACT_FORWARD_REF_TYPE;
70 export const Fragment = REACT_FRAGMENT_TYPE;
71 export const Profiler = REACT_PROFILER_TYPE;
72 export const Portal = REACT_PORTAL_TYPE;
73 +export const Memo = REACT_MEMO_TYPE;
74 export const StrictMode = REACT_STRICT_MODE_TYPE;
75
76 export {isValidElementType};
@@ -115,6 +117,9 @@ export function isFragment(object: any) {
117 export function isProfiler(object: any) {
118 return typeOf(object) === REACT_PROFILER_TYPE;
119 }
120 +export function isMemo(object: any) {
121 + return typeOf(object) === REACT_MEMO_TYPE;
122 +}
123 export function isPortal(object: any) {
124 return typeOf(object) === REACT_PORTAL_TYPE;
125 }
packages/react-is/src/__tests__/ReactIs-test.js
+8
@@ -144,6 +144,14 @@ describe('ReactIs', () => {
144 expect(ReactIs.isPortal(div)).toBe(false);
145 });
146
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 +
155 it('should identify strict mode', () => {
156 expect(ReactIs.typeOf(<React.StrictMode />)).toBe(ReactIs.StrictMode);
157 expect(ReactIs.isStrictMode(<React.StrictMode />)).toBe(true);