Skip to content

Commit 691f8f9

Browse files
committed
todo: fix fetchers
1 parent 607031f commit 691f8f9

File tree

3 files changed

+52
-50
lines changed

3 files changed

+52
-50
lines changed

packages/react-router/lib/components.tsx

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,6 @@ export function RouterProvider({
410410
unstable_transitions,
411411
}: RouterProviderProps): React.ReactElement {
412412
let [_state, setStateImpl] = React.useState(router.state);
413-
let navigationRef = React.useRef(_state.navigation);
414413
let [pending, startTransition] = React.useTransition();
415414
// @ts-expect-error - Needs React 19 types
416415
let [state, setOptimisticState] = React.useOptimistic(_state);
@@ -490,13 +489,17 @@ export function RouterProvider({
490489
} else {
491490
startTransition(() => {
492491
if (unstable_transitions === true) {
493-
if (newState.navigation.state !== "idle") {
494-
navigationRef.current = newState.navigation;
495-
}
496-
setOptimisticState((state: any) => ({
492+
// @ts-expect-error - Needs React 19 types
493+
setOptimisticState((state) => ({
497494
...state,
498-
navigation: navigationRef.current,
499-
revalidation: newState.revalidation,
495+
navigation:
496+
newState.navigation.state !== "idle"
497+
? newState.navigation
498+
: state.navigation,
499+
revalidation:
500+
newState.revalidation !== "idle"
501+
? newState.revalidation
502+
: state.revalidation,
500503
}));
501504
}
502505
logErrorsAndSetState(newState);
@@ -599,9 +602,6 @@ export function RouterProvider({
599602
} else {
600603
startTransition(() => {
601604
if (unstable_transitions === true) {
602-
if (newState.navigation.state !== "idle") {
603-
navigationRef.current = newState.navigation;
604-
}
605605
setOptimisticState((state: any) => ({
606606
...state,
607607
navigation: newState.navigation,
@@ -666,7 +666,10 @@ export function RouterProvider({
666666
const resolver = new Deferred<void>();
667667
// @ts-expect-error - Needs React 19 types
668668
startTransition(() => {
669-
return router.navigate(n).then(resolver.resolve, resolver.reject);
669+
return router
670+
.navigate(n)
671+
.then(resolver.resolve)
672+
.catch(resolver.reject);
670673
});
671674
return resolver.promise;
672675
}
@@ -752,14 +755,7 @@ export function RouterProvider({
752755
<MemoizedDataRoutes
753756
routes={router.routes}
754757
future={router.future}
755-
state={
756-
pending
757-
? {
758-
...state,
759-
navigation: navigationRef.current,
760-
}
761-
: state
762-
}
758+
state={state}
763759
unstable_onError={unstable_onError}
764760
/>
765761
</Router>

packages/react-router/lib/rsc/browser.tsx

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export function createCallServer({
138138
});
139139

140140
React.startTransition((): any =>
141-
Promise.resolve(payloadPromise)
141+
payloadPromise
142142
.then(async (payload) => {
143143
if (payload.type === "redirect") {
144144
if (payload.reload || isExternalLocation(payload.location)) {
@@ -178,14 +178,14 @@ export function createCallServer({
178178
}
179179

180180
React.startTransition(() => {
181-
let lastMatch: RSCRouteManifest | undefined;
182181
for (const match of rerender.matches) {
183-
globalVar.__reactRouterDataRouter.patchRoutes(
184-
lastMatch?.id ?? null,
182+
(
183+
window as WindowWithRouterGlobals
184+
).__reactRouterDataRouter.patchRoutes(
185+
match.parentId ?? null,
185186
[createRouteFromServerManifest(match)],
186187
true,
187188
);
188-
lastMatch = match;
189189
}
190190

191191
(
@@ -746,32 +746,38 @@ export function RSCHydratedRouter({
746746
}
747747
}, []);
748748

749-
let [location, setLocation] = React.useState(router.state.location);
749+
let [{ routes, state }, setState] = React.useState(() => ({
750+
routes: cloneRoutes(router.routes),
751+
state: router.state,
752+
}));
750753

751-
const [cachedRoutes, setCachedRoutes] = React.useState(() =>
752-
cloneRoutes(router.routes),
753-
);
754+
// const [cachedRoutes, setCachedRoutes] = React.useState(() =>
755+
// cloneRoutes(router.routes),
756+
// );
754757
// const [cachedState, setCachedState] = React.useState(router.state);
755758

756759
React.useLayoutEffect(
757760
() =>
758761
router.subscribe((newState) => {
759762
React.startTransition(() => {
760-
setCachedRoutes(cloneRoutes(router.routes));
761-
// setCachedState(newState);
762-
if (newState.location !== location) {
763-
setLocation(newState.location);
764-
}
763+
setState({
764+
routes: cloneRoutes(router.routes),
765+
state: newState,
766+
});
765767
});
766768
}),
767-
[router, location, cachedRoutes],
769+
[router, location],
768770
);
769771

770-
const transitionEnabledRouter = {
771-
...router,
772-
// state: cachedState,
773-
routes: cachedRoutes,
774-
} as typeof router;
772+
const transitionEnabledRouter = React.useMemo(
773+
() =>
774+
({
775+
...router,
776+
state,
777+
routes,
778+
}) as typeof router,
779+
[router, routes, state],
780+
);
775781

776782
React.useEffect(() => {
777783
if (
@@ -871,7 +877,7 @@ export function RSCHydratedRouter({
871877

872878
return (
873879
<RSCRouterContext.Provider value={true}>
874-
<RSCRouterGlobalErrorBoundary location={location}>
880+
<RSCRouterGlobalErrorBoundary location={state.location}>
875881
<FrameworkContext.Provider value={frameworkContext}>
876882
<RouterProvider
877883
router={transitionEnabledRouter}

playground/rsc-vite/src/square.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ export function Square() {
55
const left = Math.random() * 100;
66

77
return (
8-
<ViewTransition name="red-square">
9-
<div
10-
style={{
11-
position: "relative",
12-
height: "200px",
13-
width: "100%",
14-
}}
15-
>
8+
<div
9+
style={{
10+
position: "relative",
11+
height: "200px",
12+
width: "100%",
13+
}}
14+
>
15+
<ViewTransition name="red-square">
1616
<div
1717
style={{
1818
position: "absolute",
@@ -24,7 +24,7 @@ export function Square() {
2424
transform: "translate(-50%, -50%)",
2525
}}
2626
/>
27-
</div>
28-
</ViewTransition>
27+
</ViewTransition>
28+
</div>
2929
);
3030
}

0 commit comments

Comments
 (0)