Skip to content

Commit 7d8dd84

Browse files
aryaemami59markerikson
authored andcommitted
Update react-is implementation
- `react-is` has changed the `REACT_ELEMENT_TYPE` symbol from `'react.element'` to `'react.transitional.element'`. We want our changes to be non-breaking and backwards-compatible so we conditionally set the `REACT_ELEMENT_TYPE` based on the detected version of React.
1 parent 48ef75f commit 7d8dd84

File tree

1 file changed

+35
-57
lines changed

1 file changed

+35
-57
lines changed

src/utils/react-is.ts

Lines changed: 35 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import type { ElementType, MemoExoticComponent, ReactElement } from 'react'
2+
import * as React from 'react'
23

34
// Directly ported from:
4-
// https://unpkg.com/browse/react-is@18.3.0-canary-ee68446ff-20231115/cjs/react-is.production.js
5+
// https://unpkg.com/browse/react-is@19.0.0/cjs/react-is.production.js
56
// It's very possible this could change in the future, but given that
67
// we only use these in `connect`, this is a low priority.
78

8-
const REACT_ELEMENT_TYPE = /* @__PURE__ */ Symbol.for('react.element')
9+
export const IS_REACT_19 = /* @__PURE__ */ React.version.startsWith('19')
10+
11+
const REACT_ELEMENT_TYPE = /* @__PURE__ */ Symbol.for(
12+
IS_REACT_19 ? 'react.transitional.element' : 'react.element',
13+
)
914
const REACT_PORTAL_TYPE = /* @__PURE__ */ Symbol.for('react.portal')
1015
const REACT_FRAGMENT_TYPE = /* @__PURE__ */ Symbol.for('react.fragment')
1116
const REACT_STRICT_MODE_TYPE = /* @__PURE__ */ Symbol.for('react.strict_mode')
1217
const REACT_PROFILER_TYPE = /* @__PURE__ */ Symbol.for('react.profiler')
13-
const REACT_PROVIDER_TYPE = /* @__PURE__ */ Symbol.for('react.provider')
18+
const REACT_CONSUMER_TYPE = /* @__PURE__ */ Symbol.for('react.consumer')
1419
const REACT_CONTEXT_TYPE = /* @__PURE__ */ Symbol.for('react.context')
15-
const REACT_SERVER_CONTEXT_TYPE = /* @__PURE__ */ Symbol.for(
16-
'react.server_context',
17-
)
1820
const REACT_FORWARD_REF_TYPE = /* @__PURE__ */ Symbol.for('react.forward_ref')
1921
const REACT_SUSPENSE_TYPE = /* @__PURE__ */ Symbol.for('react.suspense')
2022
const REACT_SUSPENSE_LIST_TYPE = /* @__PURE__ */ Symbol.for(
@@ -31,87 +33,63 @@ export const ForwardRef = REACT_FORWARD_REF_TYPE
3133
export const Memo = REACT_MEMO_TYPE
3234

3335
export function isValidElementType(type: any): type is ElementType {
34-
if (typeof type === 'string' || typeof type === 'function') {
35-
return true
36-
} // Note: typeof might be other than 'symbol' or 'number' (e.g. if it's a polyfill).
37-
38-
if (
36+
return typeof type === 'string' ||
37+
typeof type === 'function' ||
3938
type === REACT_FRAGMENT_TYPE ||
4039
type === REACT_PROFILER_TYPE ||
4140
type === REACT_STRICT_MODE_TYPE ||
4241
type === REACT_SUSPENSE_TYPE ||
4342
type === REACT_SUSPENSE_LIST_TYPE ||
44-
type === REACT_OFFSCREEN_TYPE
45-
) {
46-
return true
47-
}
48-
49-
if (typeof type === 'object' && type !== null) {
50-
if (
51-
type.$$typeof === REACT_LAZY_TYPE ||
52-
type.$$typeof === REACT_MEMO_TYPE ||
53-
type.$$typeof === REACT_PROVIDER_TYPE ||
54-
type.$$typeof === REACT_CONTEXT_TYPE ||
55-
type.$$typeof === REACT_FORWARD_REF_TYPE || // This needs to include all possible module reference object
56-
// types supported by any Flight configuration anywhere since
57-
// we don't know which Flight build this will end up being used
58-
// with.
59-
type.$$typeof === REACT_CLIENT_REFERENCE ||
60-
type.getModuleId !== undefined
61-
) {
62-
return true
63-
}
64-
}
65-
66-
return false
43+
type === REACT_OFFSCREEN_TYPE ||
44+
(typeof type === 'object' &&
45+
type !== null &&
46+
(type.$$typeof === REACT_LAZY_TYPE ||
47+
type.$$typeof === REACT_MEMO_TYPE ||
48+
type.$$typeof === REACT_CONTEXT_TYPE ||
49+
type.$$typeof === REACT_CONSUMER_TYPE ||
50+
type.$$typeof === REACT_FORWARD_REF_TYPE ||
51+
type.$$typeof === REACT_CLIENT_REFERENCE ||
52+
type.getModuleId !== undefined))
53+
? !0
54+
: !1
6755
}
6856

6957
function typeOf(object: any): symbol | undefined {
7058
if (typeof object === 'object' && object !== null) {
71-
const $$typeof = object.$$typeof
59+
const { $$typeof } = object
7260

7361
switch ($$typeof) {
74-
case REACT_ELEMENT_TYPE: {
75-
const type = object.type
76-
77-
switch (type) {
62+
case REACT_ELEMENT_TYPE:
63+
switch (((object = object.type), object)) {
7864
case REACT_FRAGMENT_TYPE:
7965
case REACT_PROFILER_TYPE:
8066
case REACT_STRICT_MODE_TYPE:
8167
case REACT_SUSPENSE_TYPE:
8268
case REACT_SUSPENSE_LIST_TYPE:
83-
return type
84-
85-
default: {
86-
const $$typeofType = type && type.$$typeof
87-
88-
switch ($$typeofType) {
89-
case REACT_SERVER_CONTEXT_TYPE:
69+
return object
70+
default:
71+
switch (((object = object && object.$$typeof), object)) {
9072
case REACT_CONTEXT_TYPE:
9173
case REACT_FORWARD_REF_TYPE:
9274
case REACT_LAZY_TYPE:
9375
case REACT_MEMO_TYPE:
94-
case REACT_PROVIDER_TYPE:
95-
return $$typeofType
96-
76+
return object
77+
case REACT_CONSUMER_TYPE:
78+
return object
9779
default:
9880
return $$typeof
9981
}
100-
}
10182
}
102-
}
103-
104-
case REACT_PORTAL_TYPE: {
83+
case REACT_PORTAL_TYPE:
10584
return $$typeof
106-
}
10785
}
10886
}
109-
110-
return undefined
11187
}
11288

11389
export function isContextConsumer(object: any): object is ReactElement {
114-
return typeOf(object) === REACT_CONTEXT_TYPE
90+
return IS_REACT_19
91+
? typeOf(object) === REACT_CONSUMER_TYPE
92+
: typeOf(object) === REACT_CONTEXT_TYPE
11593
}
11694

11795
export function isMemo(object: any): object is MemoExoticComponent<any> {

0 commit comments

Comments
 (0)