From f8522255d0c91a379a0fcae7d2d40a86d2501811 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Wed, 3 Dec 2025 15:13:23 -0500 Subject: [PATCH 1/2] Use a stable useOptimistic setter stub in React 18 --- .changeset/bright-cougars-think.md | 5 +++++ packages/react-router/lib/components.tsx | 7 ++++--- 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 .changeset/bright-cougars-think.md diff --git a/.changeset/bright-cougars-think.md b/.changeset/bright-cougars-think.md new file mode 100644 index 0000000000..3bad4e49b7 --- /dev/null +++ b/.changeset/bright-cougars-think.md @@ -0,0 +1,5 @@ +--- +"react-router": patch +--- + +Use a stable useOptimistic setter stub in React 18 diff --git a/packages/react-router/lib/components.tsx b/packages/react-router/lib/components.tsx index 8ed10893e8..c61640d46a 100644 --- a/packages/react-router/lib/components.tsx +++ b/packages/react-router/lib/components.tsx @@ -77,8 +77,8 @@ import { warnOnce } from "./server-runtime/warnings"; import type { unstable_ClientInstrumentation } from "./router/instrumentation"; /** - * Webpack can fail to compile on against react versions without this export - * complains that `startTransition` doesn't exist in `React`. + * Webpack can fail to compile on against react versions without this export - + * it complains that `startTransition` doesn't exist in `React`. * * Using the string constant directly at runtime fixes the webpack build issue * but can result in terser stripping the actual call at minification time. @@ -90,6 +90,7 @@ import type { unstable_ClientInstrumentation } from "./router/instrumentation"; const USE_OPTIMISTIC = "useOptimistic"; // @ts-expect-error Needs React 19 types but we develop against 18 const useOptimisticImpl = React[USE_OPTIMISTIC]; +const stableUseOptimisticSetter = () => undefined; function useOptimisticSafe( val: T, @@ -98,7 +99,7 @@ function useOptimisticSafe( // eslint-disable-next-line react-hooks/rules-of-hooks return useOptimisticImpl(val); } else { - return [val, () => undefined]; + return [val, stableUseOptimisticSetter]; } } From 46391fb36a6930f7ba105f46be2239c00dd107c6 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Wed, 3 Dec 2025 15:20:29 -0500 Subject: [PATCH 2/2] Update comment --- packages/react-router/lib/components.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/react-router/lib/components.tsx b/packages/react-router/lib/components.tsx index c61640d46a..64e6376751 100644 --- a/packages/react-router/lib/components.tsx +++ b/packages/react-router/lib/components.tsx @@ -77,8 +77,8 @@ import { warnOnce } from "./server-runtime/warnings"; import type { unstable_ClientInstrumentation } from "./router/instrumentation"; /** - * Webpack can fail to compile on against react versions without this export - - * it complains that `startTransition` doesn't exist in `React`. + * Webpack can fail to compile against react versions without this export - + * it complains that `useOptimistic` doesn't exist in `React`. * * Using the string constant directly at runtime fixes the webpack build issue * but can result in terser stripping the actual call at minification time.