Closed silverwind closed 5 months ago
Hi @silverwind, sorry for the delay. Life kept me very busy.
Sounds like a valid request. Would this API work?
<Router onNavigate={onNavigate} ...>
with:
export type OnNavigateData = {
from: string;
to: string;
type: "push" | "replace";
};
export type OnNavigateFn = (data: OnNavigateData) => Promise<boolean>;
export interface RouterHistory {
push: (path: string) => Promise<boolean>;
replace: (path: string) => Promise<boolean>;
}
export interface RouterProps {
....
/**
* An async callback that will be called on a navigation event.
* If it resolves to false, the navigation event will be aborted
*/
onNavigate?: OnNavigateFn;
}
Sounds good. I would change the return type to accept both sync and async:
export type OnNavigateFn = (data: OnNavigateData) => Promise<boolean> | boolean;
Implementation should be the same, you just await onNavigateFn
.
A new version has been released.
To implement navigation warnings like "Are you sure to leave this unsaved page?" it would be nice if there were a way to prevent
replaceState
andpushState
, ideally in a async fashing.How about adding a
shouldAbortNavigation
option to theRouter
that accepts a function that, if present, is awaited and if it returnstrue
, abort the navigation?