File tree Expand file tree Collapse file tree 3 files changed +21
-2
lines changed
packages/react-router/lib/rsc Expand file tree Collapse file tree 3 files changed +21
-2
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " react-router " : patch
3
+ ---
4
+
5
+ Adjust internal RSC usage of ` React.use ` to avoid Webpack compilation errors when using React 18
Original file line number Diff line number Diff line change 125
125
- gatzjames
126
126
- gavriguy
127
127
- Geist5000
128
+ - GeoffKarnov
128
129
- gesposito
129
130
- gianlucca
130
131
- gijo-varghese
Original file line number Diff line number Diff line change @@ -8,6 +8,19 @@ import { RSCRouterGlobalErrorBoundary } from "./errorBoundaries";
8
8
import { shouldHydrateRouteLoader } from "../dom/ssr/routes" ;
9
9
import type { RSCPayload } from "./server.rsc" ;
10
10
11
+ // Safe version of React.use() that will not cause compilation errors against
12
+ // React 18 and will result in a runtime error if used (you can't use RSC against
13
+ // React 18).
14
+ const REACT_USE = "use" ;
15
+ const useImpl = ( React as any ) [ REACT_USE ] ;
16
+
17
+ function useSafe < T > ( promise : Promise < T > | React . Context < T > ) : T {
18
+ if ( useImpl ) {
19
+ return useImpl ( promise ) ;
20
+ }
21
+ throw new Error ( "React Router v7 requires React 19+ for RSC features." ) ;
22
+ }
23
+
11
24
export type SSRCreateFromReadableStreamFunction = (
12
25
body : ReadableStream < Uint8Array > ,
13
26
) => Promise < unknown > ;
@@ -196,8 +209,8 @@ export interface RSCStaticRouterProps {
196
209
* @returns A React component that renders the {@link unstable_RSCPayload} as HTML.
197
210
*/
198
211
export function RSCStaticRouter ( { getPayload } : RSCStaticRouterProps ) {
199
- // @ts -expect-error - need to update the React types
200
- const payload = React . use ( getPayload ( ) ) as RSCPayload ;
212
+ // Can be replaced with React.use when v18 compatibility is no longer required.
213
+ const payload = useSafe ( getPayload ( ) ) ;
201
214
202
215
if ( payload . type === "redirect" ) {
203
216
throw new Response ( null , {
You can’t perform that action at this time.
0 commit comments