Skip to content

Commit

Permalink
refactor: Simplify routeChanged a bit (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian authored Feb 13, 2025
1 parent 2344ec2 commit 34aef00
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ export function Router(props) {
// was the most recent render successful (did not suspend):
const didSuspend = /** @type {RefObject<boolean>} */ (useRef());
didSuspend.current = false;
// has the route component changed
const routeChanged = useRef(false);

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

/** @type {VNode<any> | undefined} */
let incoming = pathRoute || defaultRoute;
useMemo(() => {
const routeChanged = useMemo(() => {
prev.current = cur.current;

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

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

// Note: curChildren MUST render first in order to set didSuspend & prev.
return routeChanged.current
// Note: cur MUST render first in order to set didSuspend & prev.
return routeChanged
? [h(RenderRef, { r: cur }), h(RenderRef, { r: prev })]
: h(RenderRef, { r: cur });
}
Expand Down

0 comments on commit 34aef00

Please sign in to comment.