Skip to content

Commit 34aef00

Browse files
authored
refactor: Simplify routeChanged a bit (#66)
1 parent 2344ec2 commit 34aef00

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/router.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ export function Router(props) {
135135
// was the most recent render successful (did not suspend):
136136
const didSuspend = /** @type {RefObject<boolean>} */ (useRef());
137137
didSuspend.current = false;
138-
// has the route component changed
139-
const routeChanged = useRef(false);
140138

141139
let pathRoute, defaultRoute, matchProps;
142140
toChildArray(props.children).some((/** @type {VNode<any>} */ vnode) => {
@@ -147,7 +145,7 @@ export function Router(props) {
147145

148146
/** @type {VNode<any> | undefined} */
149147
let incoming = pathRoute || defaultRoute;
150-
useMemo(() => {
148+
const routeChanged = useMemo(() => {
151149
prev.current = cur.current;
152150

153151
// Only mark as an update if the route component changed.
@@ -156,8 +154,9 @@ export function Router(props) {
156154
// This hack prevents Preact from diffing when we swap `cur` to `prev`:
157155
if (this.__v && this.__v.__k) this.__v.__k.reverse();
158156
count.current++;
159-
routeChanged.current = true;
160-
} else routeChanged.current = false;
157+
return true;
158+
}
159+
return false;
161160
}, [url]);
162161

163162
const isHydratingSuspense = cur.current && cur.current.__u & MODE_HYDRATE && cur.current.__u & MODE_SUSPENDED;
@@ -248,8 +247,8 @@ export function Router(props) {
248247
isLoading.current = false;
249248
}, [path, wasPush, c]);
250249

251-
// Note: curChildren MUST render first in order to set didSuspend & prev.
252-
return routeChanged.current
250+
// Note: cur MUST render first in order to set didSuspend & prev.
251+
return routeChanged
253252
? [h(RenderRef, { r: cur }), h(RenderRef, { r: prev })]
254253
: h(RenderRef, { r: cur });
255254
}

0 commit comments

Comments
 (0)