mirror of https://github.com/usememos/memos.git
fix(web): memoize useNavigateTo to prevent infinite view transition loop
Wrapping the returned function in useCallback prevents a new reference on every render, which was causing an infinite startViewTransition loop on the initial signup page (fresh install with no admin). Fixes #5626
This commit is contained in:
parent
6eaab52e48
commit
2ab476ef2e
|
|
@ -1,18 +1,22 @@
|
|||
import { useCallback } from "react";
|
||||
import { NavigateOptions, useNavigate } from "react-router-dom";
|
||||
|
||||
const useNavigateTo = () => {
|
||||
const navigateTo = useNavigate();
|
||||
|
||||
const navigateToWithViewTransition = (to: string, options?: NavigateOptions) => {
|
||||
const doc = window.document as unknown as Document & { startViewTransition?: (callback: () => void) => void };
|
||||
if (!doc.startViewTransition) {
|
||||
navigateTo(to, options);
|
||||
} else {
|
||||
document.startViewTransition(() => {
|
||||
const navigateToWithViewTransition = useCallback(
|
||||
(to: string, options?: NavigateOptions) => {
|
||||
const doc = window.document as unknown as Document & { startViewTransition?: (callback: () => void) => void };
|
||||
if (!doc.startViewTransition) {
|
||||
navigateTo(to, options);
|
||||
});
|
||||
}
|
||||
};
|
||||
} else {
|
||||
document.startViewTransition(() => {
|
||||
navigateTo(to, options);
|
||||
});
|
||||
}
|
||||
},
|
||||
[navigateTo],
|
||||
);
|
||||
|
||||
return navigateToWithViewTransition;
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue