Skip to content

Commit c5d9e25

Browse files
authored
Fix feature detection of React.startTransition (#10569)
1 parent 2544267 commit c5d9e25

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

.changeset/silly-eagles-divide.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"react-router": patch
3+
"react-router-dom": patch
4+
---
5+
6+
Adjust feature detection of `React.startTransition` to fix webpack + react 17 compilation error

packages/react-router-dom/index.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ export interface BrowserRouterProps {
300300
window?: Window;
301301
}
302302

303+
// Webpack + React 17 fails to compile on the usage of `React.startTransition` or
304+
// `React["startTransition"]` even if it's behind a feature detection of
305+
// `"startTransition" in React`. Moving this to a constant avoids the issue :/
306+
const START_TRANSITION = "startTransition";
307+
303308
/**
304309
* A `<Router>` for use in web browsers. Provides the cleanest URLs.
305310
*/
@@ -320,8 +325,8 @@ export function BrowserRouter({
320325
});
321326
let setState = React.useCallback(
322327
(newState: { action: NavigationType; location: Location }) => {
323-
"startTransition" in React
324-
? React.startTransition(() => setStateImpl(newState))
328+
START_TRANSITION in React
329+
? React[START_TRANSITION](() => setStateImpl(newState))
325330
: setStateImpl(newState);
326331
},
327332
[setStateImpl]
@@ -363,8 +368,8 @@ export function HashRouter({ basename, children, window }: HashRouterProps) {
363368
});
364369
let setState = React.useCallback(
365370
(newState: { action: NavigationType; location: Location }) => {
366-
"startTransition" in React
367-
? React.startTransition(() => setStateImpl(newState))
371+
START_TRANSITION in React
372+
? React[START_TRANSITION](() => setStateImpl(newState))
368373
: setStateImpl(newState);
369374
},
370375
[setStateImpl]
@@ -402,8 +407,8 @@ function HistoryRouter({ basename, children, history }: HistoryRouterProps) {
402407
});
403408
let setState = React.useCallback(
404409
(newState: { action: NavigationType; location: Location }) => {
405-
"startTransition" in React
406-
? React.startTransition(() => setStateImpl(newState))
410+
START_TRANSITION in React
411+
? React[START_TRANSITION](() => setStateImpl(newState))
407412
: setStateImpl(newState);
408413
},
409414
[setStateImpl]

packages/react-router/lib/components.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ export interface RouterProviderProps {
5454
router: RemixRouter;
5555
}
5656

57+
// Webpack + React 17 fails to compile on the usage of `React.startTransition` or
58+
// `React["startTransition"]` even if it's behind a feature detection of
59+
// `"startTransition" in React`. Moving this to a constant avoids the issue :/
60+
const START_TRANSITION = "startTransition";
61+
5762
/**
5863
* Given a Remix Router instance, render the appropriate UI
5964
*/
@@ -66,8 +71,8 @@ export function RouterProvider({
6671
let [state, setStateImpl] = React.useState(router.state);
6772
let setState = React.useCallback(
6873
(newState: RouterState) => {
69-
"startTransition" in React
70-
? React.startTransition(() => setStateImpl(newState))
74+
START_TRANSITION in React
75+
? React[START_TRANSITION](() => setStateImpl(newState))
7176
: setStateImpl(newState);
7277
},
7378
[setStateImpl]
@@ -178,8 +183,8 @@ export function MemoryRouter({
178183
});
179184
let setState = React.useCallback(
180185
(newState: { action: NavigationType; location: Location }) => {
181-
"startTransition" in React
182-
? React.startTransition(() => setStateImpl(newState))
186+
START_TRANSITION in React
187+
? React[START_TRANSITION](() => setStateImpl(newState))
183188
: setStateImpl(newState);
184189
},
185190
[setStateImpl]

0 commit comments

Comments
 (0)