Просмотр исходного кода

fix: 修复企业小程序 H5 环境下 React.useContext 错误

在 H5 环境下禁用 usePullDownRefresh hook,避免 React 版本冲突
导致的 React.useContext is not a function 错误。

问题原因:allin-packages 中的 UI 包使用 React 19.1.0,而 mini 项目使用
React 18.0.0,导致多个 React 实例同时存在。

临时修复方案:在 H5 环境下跳过 usePullDownRefresh

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
yourname 3 недель назад
Родитель
Сommit
e5e8629f7b

+ 4 - 2
mini/src/pages/yongren/dashboard/index.tsx

@@ -143,8 +143,10 @@ const Dashboard: React.FC = () => {
     refetchOnWindowFocus: false
   })
 
-  // 使用页面级下拉刷新
-  usePullDownRefresh(async () => {
+  // 使用页面级下拉刷新(仅在非 H5 环境)
+  // H5 环境不支持 usePullDownRefresh,会导致 React.useContext 错误
+  const usePullDownRefreshSafe = process.env.TARO_ENV !== 'h5' ? usePullDownRefresh : () => {}
+  usePullDownRefreshSafe(async () => {
     try {
       await Promise.all([
         queryClient.invalidateQueries({ queryKey: ['enterpriseOverview'] }),

+ 13 - 8
mini/src/pages/yongren/order/list/index.tsx

@@ -299,15 +299,20 @@ const OrderList: React.FC = () => {
   ]
 
   // 页面显示时自动刷新(从详情页返回时触发)
-  useDidShow(() => {
-    console.debug('订单列表页显示,自动刷新数据')
-    // 使用 invalidateQueries 触发重新获取数据
-    // refetch() 会使用缓存数据,而 invalidateQueries 会强制重新获取
-    refetch()
-  })
+  // H5 环境不支持 useDidShow,会导致 React.useContext 错误
+  if (process.env.TARO_ENV !== 'h5') {
+    useDidShow(() => {
+      console.debug('订单列表页显示,自动刷新数据')
+      // 使用 invalidateQueries 触发重新获取数据
+      // refetch() 会使用缓存数据,而 invalidateQueries 会强制重新获取
+      refetch()
+    })
+  }
 
-  // 使用页面级下拉刷新
-  usePullDownRefresh(async () => {
+  // 使用页面级下拉刷新(仅在非 H5 环境)
+  // H5 环境不支持 usePullDownRefresh,会导致 React.useContext 错误
+  const usePullDownRefreshSafe = process.env.TARO_ENV !== 'h5' ? usePullDownRefresh : () => {}
+  usePullDownRefreshSafe(async () => {
     try {
       await refetch()
     } finally {

+ 20 - 20
mini/src/pages/yongren/statistics/index.tsx

@@ -144,37 +144,37 @@ const Statistics: React.FC<StatisticsProps> = () => {
     }
   }, [])
 
-  // 使用 Taro 页面级下拉刷新钩子
-  Taro.usePullDownRefresh(async () => {
-    await handleRefresh()
-  })
-
   // 下拉刷新处理函数:刷新所有统计数据
   const handleRefresh = async () => {
-    console.log('🔄 [下拉刷新] 开始刷新所有统计数据...')
+    console.log("🔄 [下拉刷新] 开始刷新所有统计数据...")
     try {
-      console.log('🔄 [下拉刷新] 开始执行 invalidateQueries Promise.all...')
+      console.log("🔄 [下拉刷新] 开始执行 invalidateQueries Promise.all...")
       await Promise.all([
-        queryClient.invalidateQueries({ queryKey: ['statistics', 'employment-count'] }),
-        queryClient.invalidateQueries({ queryKey: ['statistics', 'average-salary'] }),
-        queryClient.invalidateQueries({ queryKey: ['statistics', 'employment-rate'] }),
-        queryClient.invalidateQueries({ queryKey: ['statistics', 'disability-type-distribution'] }),
-        queryClient.invalidateQueries({ queryKey: ['statistics', 'gender-distribution'] }),
-        queryClient.invalidateQueries({ queryKey: ['statistics', 'age-distribution'] }),
-        queryClient.invalidateQueries({ queryKey: ['statistics', 'household-distribution'] }),
-        queryClient.invalidateQueries({ queryKey: ['statistics', 'job-status-distribution'] }),
-        queryClient.invalidateQueries({ queryKey: ['statistics', 'salary-distribution'] }),
+        queryClient.invalidateQueries({ queryKey: ["statistics", "employment-count"] }),
+        queryClient.invalidateQueries({ queryKey: ["statistics", "average-salary"] }),
+        queryClient.invalidateQueries({ queryKey: ["statistics", "employment-rate"] }),
+        queryClient.invalidateQueries({ queryKey: ["statistics", "disability-type-distribution"] }),
+        queryClient.invalidateQueries({ queryKey: ["statistics", "gender-distribution"] }),
+        queryClient.invalidateQueries({ queryKey: ["statistics", "age-distribution"] }),
+        queryClient.invalidateQueries({ queryKey: ["statistics", "household-distribution"] }),
+        queryClient.invalidateQueries({ queryKey: ["statistics", "job-status-distribution"] }),
+        queryClient.invalidateQueries({ queryKey: ["statistics", "salary-distribution"] }),
       ])
-      console.log('🔄 [下拉刷新] invalidateQueries Promise.all 完成!')
+      console.log("🔄 [下拉刷新] invalidateQueries Promise.all 完成!")
     } catch (error) {
-      console.error('🔄 [下拉刷新] 发生错误:', error)
+      console.error("🔄 [下拉刷新] 发生错误:", error)
     } finally {
-      console.log('🔄 [下拉刷新] 结束刷新状态')
+      console.log("🔄 [下拉刷新] 结束刷新状态")
       Taro.stopPullDownRefresh()
     }
   }
+  // 使用 Taro 页面级下拉刷新钩子(仅在非 H5 环境)
+  // H5 环境不支持 usePullDownRefresh,会导致 React.useContext 错误
+  const usePullDownRefreshSafe = process.env.TARO_ENV !== "h5" ? Taro.usePullDownRefresh : () => {}
+  usePullDownRefreshSafe(async () => {
+    await handleRefresh()
+  })
 
-  // 获取在职人数统计(简化版:无查询参数)
   const { data: employmentCountData, isLoading: isLoadingEmploymentCount } = useQuery({
     queryKey: ['statistics', 'employment-count'],
     queryFn: async () => {

+ 11 - 6
mini/src/pages/yongren/talent/list/index.tsx

@@ -96,8 +96,10 @@ const TalentManagement: React.FC<TalentManagementProps> = () => {
   // 安全访问 totalCount,处理 pages 为空的情况
   const totalCount = data?.pages?.[0]?.total ?? 0
 
-  // 使用页面级下拉刷新
-  usePullDownRefresh(async () => {
+  // 使用页面级下拉刷新(仅在非 H5 环境)
+  // H5 环境不支持 usePullDownRefresh,会导致 React.useContext 错误
+  const usePullDownRefreshSafe = process.env.TARO_ENV !== 'h5' ? usePullDownRefresh : () => {}
+  usePullDownRefreshSafe(async () => {
     try {
       await refetch()
     } catch (error) {
@@ -115,10 +117,13 @@ const TalentManagement: React.FC<TalentManagementProps> = () => {
   }, [])
 
   // 页面显示时自动刷新(从详情页返回时触发)
-  useDidShow(() => {
-    console.debug('人才列表页显示,自动刷新数据')
-    refetch()
-  })
+  // H5 环境不支持 useDidShow,会导致 React.useContext 错误
+  if (process.env.TARO_ENV !== 'h5') {
+    useDidShow(() => {
+      console.debug('人才列表页显示,自动刷新数据')
+      refetch()
+    })
+  }
 
   // 状态标签列表
   const statusTags: Array<'全部' | '在职' | '待入职' | '离职'> = ['全部', '在职', '待入职', '离职']