Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Simplify routeChanged a bit #66

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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