fix(mp): setTimeout 无清理修复 — useSafeTimeout hook + 10 页面接入

新增 useSafeTimeout hook,页面隐藏时自动清理所有定时器。
10 个页面接入:daily-monitoring、exchange、family-add、
health/input、prescription detail/create、dialysis detail/create、
appointment detail/create。所有 fire-and-forget setTimeout 替换为
safeSetTimeout,避免页面切走后定时器回调在错误上下文执行。
This commit is contained in:
iven
2026-05-15 00:38:23 +08:00
parent 74bffb4878
commit fed1759985
11 changed files with 57 additions and 14 deletions

View File

@@ -3,6 +3,7 @@ import { View, Text, Input, Picker } from '@tarojs/components';
import Taro, { useRouter } from '@tarojs/taro';
import { createPatient, updatePatient, Patient } from '../../../services/patient';
import { useElderClass } from '../../../hooks/useElderClass';
import { useSafeTimeout } from '@/hooks/useSafeTimeout';
import './index.scss';
const RELATION_OPTIONS = ['本人', '配偶', '父母', '子女', '其他'];
@@ -23,6 +24,7 @@ export default function FamilyAdd() {
);
const [birthDate, setBirthDate] = useState(editData?.birth_date || '');
const [submitting, setSubmitting] = useState(false);
const { safeSetTimeout } = useSafeTimeout();
useEffect(() => {
return () => { Taro.removeStorageSync('edit_patient'); };
@@ -54,7 +56,7 @@ export default function FamilyAdd() {
Taro.hideLoading();
Taro.showToast({ title: '添加成功', icon: 'success' });
}
setTimeout(() => Taro.navigateBack(), 1000);
safeSetTimeout(() => Taro.navigateBack(), 1000);
} catch {
Taro.hideLoading();
Taro.showToast({ title: editId ? '修改失败' : '添加失败', icon: 'none' });