Skip to content

Commit

Permalink
Fix feature detection of React.startTransition
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Jun 7, 2023
1 parent 610ce6e commit 6069e2a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changeset/silly-eagles-divide.md
@@ -0,0 +1,6 @@
---
"react-router": patch
"react-router-dom": patch
---

Adjust feature detection of `React.startTransition` to fix webpack + react 17 compilation error
17 changes: 11 additions & 6 deletions packages/react-router-dom/index.tsx
Expand Up @@ -300,6 +300,11 @@ export interface BrowserRouterProps {
window?: Window;
}

// Webpack + React 17 fails to compile on the usage of `React.startTransition` or
// `React["startTransition"]` even if it's behind a feature detection of
// `"startTransition" in React`. Moving this to a constant avoids the issue :/
const START_TRANSITION = "startTransition";

/**
* A `<Router>` for use in web browsers. Provides the cleanest URLs.
*/
Expand All @@ -320,8 +325,8 @@ export function BrowserRouter({
});
let setState = React.useCallback(
(newState: { action: NavigationType; location: Location }) => {
"startTransition" in React
? React.startTransition(() => setStateImpl(newState))
START_TRANSITION in React
? React[START_TRANSITION](() => setStateImpl(newState))
: setStateImpl(newState);
},
[setStateImpl]
Expand Down Expand Up @@ -363,8 +368,8 @@ export function HashRouter({ basename, children, window }: HashRouterProps) {
});
let setState = React.useCallback(
(newState: { action: NavigationType; location: Location }) => {
"startTransition" in React
? React.startTransition(() => setStateImpl(newState))
START_TRANSITION in React
? React[START_TRANSITION](() => setStateImpl(newState))
: setStateImpl(newState);
},
[setStateImpl]
Expand Down Expand Up @@ -402,8 +407,8 @@ function HistoryRouter({ basename, children, history }: HistoryRouterProps) {
});
let setState = React.useCallback(
(newState: { action: NavigationType; location: Location }) => {
"startTransition" in React
? React.startTransition(() => setStateImpl(newState))
START_TRANSITION in React
? React[START_TRANSITION](() => setStateImpl(newState))
: setStateImpl(newState);
},
[setStateImpl]
Expand Down
13 changes: 9 additions & 4 deletions packages/react-router/lib/components.tsx
Expand Up @@ -54,6 +54,11 @@ export interface RouterProviderProps {
router: RemixRouter;
}

// Webpack + React 17 fails to compile on the usage of `React.startTransition` or
// `React["startTransition"]` even if it's behind a feature detection of
// `"startTransition" in React`. Moving this to a constant avoids the issue :/
const START_TRANSITION = "startTransition";

/**
* Given a Remix Router instance, render the appropriate UI
*/
Expand All @@ -66,8 +71,8 @@ export function RouterProvider({
let [state, setStateImpl] = React.useState(router.state);
let setState = React.useCallback(
(newState: RouterState) => {
"startTransition" in React
? React.startTransition(() => setStateImpl(newState))
START_TRANSITION in React
? React[START_TRANSITION](() => setStateImpl(newState))
: setStateImpl(newState);
},
[setStateImpl]
Expand Down Expand Up @@ -178,8 +183,8 @@ export function MemoryRouter({
});
let setState = React.useCallback(
(newState: { action: NavigationType; location: Location }) => {
"startTransition" in React
? React.startTransition(() => setStateImpl(newState))
START_TRANSITION in React
? React[START_TRANSITION](() => setStateImpl(newState))
: setStateImpl(newState);
},
[setStateImpl]
Expand Down

0 comments on commit 6069e2a

Please sign in to comment.