Files
dukang/apps/mini-user/src/lib/patch-taro-h5-hooks.ts
T
jacy 5a2e8dfcf8
CI / verify (pull_request) Has been cancelled
fix(mini-user): dedupe Taro reactMeta so H5 useDidShow works
Vite was bundling plugin-framework-react runtime twice, so tab pages crashed with useContext is not a function on enter/switch.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-15 19:33:51 +08:00

70 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* H5Taro Vite 偶发把 @tarojs/plugin-framework-react/dist/runtime 打成两份,
* 导致 createReactApp 初始化了 B 份 reactMeta,而 Taro.useDidShow 仍绑定 A 份(R={}),
* 页面一进就报 n.useContext is not a function。
*
* 在 App 渲染前,用与 createReactApp 同一份 runtime 的 hooks 覆盖到 Taro 上。
*/
import Taro from '@tarojs/taro';
import {
useAddToFavorites,
useDidHide,
useDidShow,
useError,
useKeyboardHeight,
useLaunch,
useLoad,
useOptionMenuClick,
usePageNotFound,
usePageScroll,
usePullDownRefresh,
usePullIntercept,
useReachBottom,
useReady,
useResize,
useRouter,
useSaveExitState,
useScope,
useShareAppMessage,
useShareTimeline,
useTabItemTap,
useTitleClick,
useUnhandledRejection,
useUnload,
} from '@tarojs/plugin-framework-react/dist/runtime';
const HOOKS = {
useAddToFavorites,
useDidHide,
useDidShow,
useError,
useKeyboardHeight,
useLaunch,
useLoad,
useOptionMenuClick,
usePageNotFound,
usePageScroll,
usePullDownRefresh,
usePullIntercept,
useReachBottom,
useReady,
useResize,
useRouter,
useSaveExitState,
useScope,
useShareAppMessage,
useShareTimeline,
useTabItemTap,
useTitleClick,
useUnhandledRejection,
useUnload,
} as const;
export function patchTaroH5Hooks() {
if (process.env.TARO_ENV !== 'h5') return;
const target = Taro as unknown as Record<string, unknown>;
for (const [key, fn] of Object.entries(HOOKS)) {
target[key] = fn;
}
}