Skip to content

Commit 143477d

Browse files
committed
fix(react-v19): Properly support react v19
Make prepass backwards compatible with react v19, it was necessary to handle with the following changes in React: - facebook/react#28789 - facebook/react#28813 - facebook/react#28226
1 parent b19a571 commit 143477d

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

.tool-versions

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nodejs 14.20.0
1+
nodejs 18.19.0

src/element.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { AbstractContext, AbstractElement } from './types'
66
import {
77
type ReactSymbol,
88
REACT_ELEMENT_TYPE,
9+
REACT_TRANSITIONAL_ELEMENT_TYPE,
910
REACT_PORTAL_TYPE,
1011
REACT_FRAGMENT_TYPE,
1112
REACT_STRICT_MODE_TYPE,
@@ -29,6 +30,7 @@ export const typeOf = (x: AbstractElement): ReactSymbol | void => {
2930
case REACT_PORTAL_TYPE:
3031
return REACT_PORTAL_TYPE
3132
case REACT_ELEMENT_TYPE:
33+
case REACT_TRANSITIONAL_ELEMENT_TYPE:
3234
switch (x.type) {
3335
case REACT_CONCURRENT_MODE_TYPE:
3436
return REACT_CONCURRENT_MODE_TYPE
@@ -48,7 +50,6 @@ export const typeOf = (x: AbstractElement): ReactSymbol | void => {
4850
case REACT_MEMO_TYPE:
4951
return REACT_MEMO_TYPE
5052
case REACT_CONTEXT_TYPE:
51-
return REACT_CONTEXT_TYPE
5253
case REACT_PROVIDER_TYPE:
5354
return REACT_PROVIDER_TYPE
5455
case REACT_FORWARD_REF_TYPE:

src/symbols.js

+9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
import type { Node } from 'react'
44

5+
/**
6+
* Element is already legacy in NextJS v15 https://github.com/vercel/next.js/pull/65058
7+
* https://github.com/facebook/react/pull/28813
8+
*/
59
let Element = 0xeac7
10+
let TransitionalElement = 0xeac7
611
let Portal = 0xeaca
712
let Fragment = 0xeacb
813
let StrictMode = 0xeacc
@@ -19,6 +24,7 @@ let ClientReferenceTag = undefined
1924
if (typeof Symbol === 'function' && Symbol.for) {
2025
const symbolFor = Symbol.for
2126
Element = symbolFor('react.element')
27+
TransitionalElement = symbolFor('react.transitional.element')
2228
Portal = symbolFor('react.portal')
2329
Fragment = symbolFor('react.fragment')
2430
StrictMode = symbolFor('react.strict_mode')
@@ -36,6 +42,7 @@ if (typeof Symbol === 'function' && Symbol.for) {
3642
/** Literal types representing the ReactSymbol values. These values do not actually match the values from react-is! */
3743
export type ReactSymbol =
3844
| 'react.element' /* 0xeac7 | Symbol(react.element) */
45+
| 'react.transitional.element' /* 0xeac7 | Symbol(react.transitional.element) */
3946
| 'react.portal' /* 0xeaca | Symbol(react.portal) */
4047
| 'react.fragment' /* 0xeacb | Symbol(react.fragment) */
4148
| 'react.strict_mode' /* 0xeacc | Symbol(react.strict_mode) */
@@ -49,6 +56,8 @@ export type ReactSymbol =
4956
| 'react.lazy' /* 0xead4 | Symbol(react.lazy) */
5057

5158
export const REACT_ELEMENT_TYPE: 'react.element' = (Element: any)
59+
export const REACT_TRANSITIONAL_ELEMENT_TYPE: 'react.transitional.element' =
60+
(TransitionalElement: any)
5261
export const REACT_PORTAL_TYPE: 'react.portal' = (Portal: any)
5362
export const REACT_FRAGMENT_TYPE: 'react.fragment' = (Fragment: any)
5463
export const REACT_STRICT_MODE_TYPE: 'react.strict_mode' = (StrictMode: any)

src/visitor.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import {
6666

6767
import {
6868
REACT_ELEMENT_TYPE,
69+
REACT_TRANSITIONAL_ELEMENT_TYPE,
6970
REACT_PORTAL_TYPE,
7071
REACT_FRAGMENT_TYPE,
7172
REACT_STRICT_MODE_TYPE,
@@ -86,7 +87,19 @@ const REACT_INTERNALS =
8687
(React: any).__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE
8788

8889
const ReactCurrentDispatcher =
89-
REACT_INTERNALS && REACT_INTERNALS.ReactCurrentDispatcher
90+
REACT_INTERNALS.ReactCurrentDispatcher || REACT_INTERNALS
91+
92+
const getReactCurrentDispatcher = () => {
93+
return ReactCurrentDispatcher.current || ReactCurrentDispatcher.H
94+
}
95+
96+
const injectReactCurrentDispatcher = (newDispatcher) => {
97+
if (ReactCurrentDispatcher.current) {
98+
ReactCurrentDispatcher.current = newDispatcher
99+
} else {
100+
ReactCurrentDispatcher.H = newDispatcher
101+
}
102+
}
90103

91104
// In the presence of setImmediate, i.e. on Node, we'll enable the
92105
// yielding behavior that gives the event loop a chance to continue
@@ -141,7 +154,9 @@ export const visitElement = (
141154
const providerElement = ((element: any): ProviderElement)
142155
// Add provider's value prop to context
143156
const { value, children } = providerElement.props
144-
setContextValue(providerElement.type._context, value)
157+
const type = (providerElement.type: any)
158+
const context = typeof type._context === 'object' ? type._context : type
159+
setContextValue(context, value)
145160

146161
return getChildrenArray(children)
147162
}
@@ -221,11 +236,11 @@ const visitLoop = (
221236
visitor: Visitor,
222237
clientRefVisitor: ClientReferenceVisitor
223238
): boolean => {
224-
const prevDispatcher = ReactCurrentDispatcher.current
239+
const prevDispatcher = getReactCurrentDispatcher()
225240
const start = Date.now()
226241

227242
try {
228-
ReactCurrentDispatcher.current = Dispatcher
243+
injectReactCurrentDispatcher(Dispatcher)
229244
while (traversalChildren.length > 0) {
230245
const element = traversalChildren[traversalChildren.length - 1].shift()
231246
if (element !== undefined) {
@@ -254,7 +269,7 @@ const visitLoop = (
254269
queue.unshift(errorFrame)
255270
return false
256271
} finally {
257-
ReactCurrentDispatcher.current = prevDispatcher
272+
injectReactCurrentDispatcher(prevDispatcher)
258273
}
259274
}
260275

@@ -341,10 +356,10 @@ export const update = (
341356
)
342357
}
343358
} else {
344-
const prevDispatcher = ReactCurrentDispatcher.current
359+
const prevDispatcher = getReactCurrentDispatcher()
345360
let children = null
346361

347-
ReactCurrentDispatcher.current = Dispatcher
362+
injectReactCurrentDispatcher(Dispatcher)
348363

349364
try {
350365
if (frame.kind === 'frame.class') {
@@ -363,7 +378,7 @@ export const update = (
363378
queue.unshift(errorFrame)
364379
children = null
365380
} finally {
366-
ReactCurrentDispatcher.current = prevDispatcher
381+
injectReactCurrentDispatcher(prevDispatcher)
367382
}
368383

369384
visit(getChildrenArray(children), queue, visitor, clientRefVisitor)

0 commit comments

Comments
 (0)