Skip to content

Commit dac4129

Browse files
authored
Use a stable useOptimistic setter stub in React 18 (#14628)
1 parent a618b58 commit dac4129

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

.changeset/bright-cougars-think.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
Use a stable useOptimistic setter stub in React 18

packages/react-router/lib/components.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ import { warnOnce } from "./server-runtime/warnings";
7777
import type { unstable_ClientInstrumentation } from "./router/instrumentation";
7878

7979
/**
80-
* Webpack can fail to compile on against react versions without this export
81-
* complains that `startTransition` doesn't exist in `React`.
80+
* Webpack can fail to compile against react versions without this export -
81+
* it complains that `useOptimistic` doesn't exist in `React`.
8282
*
8383
* Using the string constant directly at runtime fixes the webpack build issue
8484
* but can result in terser stripping the actual call at minification time.
@@ -90,6 +90,7 @@ import type { unstable_ClientInstrumentation } from "./router/instrumentation";
9090
const USE_OPTIMISTIC = "useOptimistic";
9191
// @ts-expect-error Needs React 19 types but we develop against 18
9292
const useOptimisticImpl = React[USE_OPTIMISTIC];
93+
const stableUseOptimisticSetter = () => undefined;
9394

9495
function useOptimisticSafe<T>(
9596
val: T,
@@ -98,7 +99,7 @@ function useOptimisticSafe<T>(
9899
// eslint-disable-next-line react-hooks/rules-of-hooks
99100
return useOptimisticImpl(val);
100101
} else {
101-
return [val, () => undefined];
102+
return [val, stableUseOptimisticSetter];
102103
}
103104
}
104105

0 commit comments

Comments
 (0)