From e0c334b5ed869a3412de20a543f90b8aa07b97d8 Mon Sep 17 00:00:00 2001 From: icarus Date: Tue, 14 Oct 2025 17:34:15 +0800 Subject: [PATCH] fix(translate): use state instead of ref as hook parameter fix: Error: Cannot access refs during render --- .../selection/action/components/ActionTranslate.tsx | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/renderer/src/windows/selection/action/components/ActionTranslate.tsx b/src/renderer/src/windows/selection/action/components/ActionTranslate.tsx index 0d29687787..13e5fa4443 100644 --- a/src/renderer/src/windows/selection/action/components/ActionTranslate.tsx +++ b/src/renderer/src/windows/selection/action/components/ActionTranslate.tsx @@ -41,14 +41,20 @@ const ActionTranslate: FC = ({ action, scrollToBottom }) => { const [isContented, setIsContented] = useState(false) const [isLoading, setIsLoading] = useState(true) const [contentToCopy, setContentToCopy] = useState('') + const [topic, setTopic] = useState(null) const { getLanguageByLangcode } = useTranslate() // Use useRef for values that shouldn't trigger re-renders const initialized = useRef(false) const assistantRef = useRef(null) - const topicRef = useRef(null) + const topicRef = useRef(topic) const askId = useRef('') + // update ref + useEffect(() => { + topicRef.current = topic + }, [topic]) + useEffect(() => { runAsyncFunction(async () => { const biDirectionLangPair = await db.settings.get({ id: 'translate:bidirectional:pair' }) @@ -147,7 +153,7 @@ const ActionTranslate: FC = ({ action, scrollToBottom }) => { fetchResult() }, [fetchResult]) - const allMessages = useTopicMessages(topicRef.current?.id || '') + const allMessages = useTopicMessages(topic?.id || '') const messageContent = useMemo(() => { const assistantMessages = allMessages.filter((message) => message.role === 'assistant')