From 631336097fec583391df382418f3c77b3f3b45e2 Mon Sep 17 00:00:00 2001 From: hzm <934585316@qq.com> Date: Fri, 13 Mar 2026 10:14:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=AE=BE=E7=BD=AE=E6=8C=81=E4=BB=93?= =?UTF-8?q?=E8=BE=93=E5=85=A5=E5=9B=9E=E6=BB=9A=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/components/HoldingEditModal.jsx | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/app/components/HoldingEditModal.jsx b/app/components/HoldingEditModal.jsx index 3af822d..c3fe8ea 100644 --- a/app/components/HoldingEditModal.jsx +++ b/app/components/HoldingEditModal.jsx @@ -1,6 +1,6 @@ 'use client'; -import { useEffect, useState } from 'react'; +import { useEffect, useMemo, useRef, useState } from 'react'; import { CloseIcon, SettingsIcon } from './Icons'; import { Dialog, @@ -12,12 +12,21 @@ export default function HoldingEditModal({ fund, holding, onClose, onSave }) { const [mode, setMode] = useState('amount'); // 'amount' | 'share' const dwjz = fund?.dwjz || fund?.gsz || 0; + const dwjzRef = useRef(dwjz); + useEffect(() => { + dwjzRef.current = dwjz; + }, [dwjz]); const [share, setShare] = useState(''); const [cost, setCost] = useState(''); const [amount, setAmount] = useState(''); const [profit, setProfit] = useState(''); + const holdingSig = useMemo(() => { + if (!holding) return ''; + return `${holding.id ?? ''}|${holding.share ?? ''}|${holding.cost ?? ''}`; + }, [holding]); + useEffect(() => { if (holding) { const s = holding.share || 0; @@ -25,14 +34,17 @@ export default function HoldingEditModal({ fund, holding, onClose, onSave }) { setShare(String(s)); setCost(String(c)); - if (dwjz > 0) { - const a = s * dwjz; - const p = (dwjz - c) * s; + const price = dwjzRef.current; + if (price > 0) { + const a = s * price; + const p = (price - c) * s; setAmount(a.toFixed(2)); setProfit(p.toFixed(2)); } } - }, [holding, fund, dwjz]); + // 只在“切换持仓/初次打开”时初始化,避免净值刷新覆盖用户输入 + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [holdingSig]); const handleModeChange = (newMode) => { if (newMode === mode) return;