ce0765b298
H5 新增 navigation.canGoBack hook 并在直达二级页时补齐返回锚点 路由导航保留完整原生宿主上下文并标记应用历史状态 补齐 HostBridge 返回栈消费测试、门禁和文档
64 lines
1.4 KiB
TypeScript
64 lines
1.4 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
|
|
import {
|
|
canUseNativeHostCapability,
|
|
subscribeHostNavigationCanGoBack,
|
|
subscribeHostRuntimeChange,
|
|
} from '../services/host-bridge/hostBridge';
|
|
|
|
function canUseHostNavigationCanGoBack() {
|
|
return (
|
|
canUseNativeHostCapability('host.events') &&
|
|
canUseNativeHostCapability('navigation.canGoBack')
|
|
);
|
|
}
|
|
|
|
export function useHostNavigationCanGoBack() {
|
|
const [isSupported, setIsSupported] = useState(() =>
|
|
canUseHostNavigationCanGoBack(),
|
|
);
|
|
const [canGoBack, setCanGoBack] = useState(false);
|
|
|
|
useEffect(() => {
|
|
let isDisposed = false;
|
|
let unsubscribeNavigation: () => void = () => {};
|
|
|
|
const syncSubscription = () => {
|
|
unsubscribeNavigation();
|
|
unsubscribeNavigation = () => {};
|
|
|
|
const nextIsSupported = canUseHostNavigationCanGoBack();
|
|
if (!isDisposed) {
|
|
setIsSupported(nextIsSupported);
|
|
}
|
|
|
|
if (!nextIsSupported) {
|
|
if (!isDisposed) {
|
|
setCanGoBack(false);
|
|
}
|
|
return;
|
|
}
|
|
|
|
unsubscribeNavigation = subscribeHostNavigationCanGoBack((payload) => {
|
|
if (!isDisposed) {
|
|
setCanGoBack(payload.canGoBack);
|
|
}
|
|
});
|
|
};
|
|
|
|
syncSubscription();
|
|
const unsubscribeRuntime = subscribeHostRuntimeChange(syncSubscription);
|
|
|
|
return () => {
|
|
isDisposed = true;
|
|
unsubscribeRuntime();
|
|
unsubscribeNavigation();
|
|
};
|
|
}, []);
|
|
|
|
return {
|
|
canGoBack,
|
|
isSupported,
|
|
};
|
|
}
|