fix: 打開不同對話時, 使用了同一個 ChatFragment

* 並修復了使用 key 時, 因爲卸載組件后 ref 丟失導致的錯誤
This commit is contained in:
CrescentLeaf
2025-09-25 16:26:46 +08:00
parent 4b93e5fd67
commit a928577f2a
3 changed files with 4 additions and 2 deletions

View File

@@ -173,7 +173,8 @@ export default function App() {
}
{
isShowChatFragment && <ChatFragment
target={currentChatId} />
target={currentChatId}
key={currentChatId} />
}
</div>
</div>

View File

@@ -109,6 +109,7 @@ export default function AppMobile() {
<ChatFragment
showReturnButton={true}
onReturnButtonClicked={() => setIsShowChatFragment(false)}
key={currentChatId}
target={currentChatId} />
</div>
</mdui-dialog>

View File

@@ -3,6 +3,6 @@ import * as React from 'react'
export default function useEventListener<T extends HTMLElement | null>(ref: React.MutableRefObject<T>, eventName: string, callback: (event: Event) => void) {
React.useEffect(() => {
ref.current!.addEventListener(eventName, callback)
return () => ref.current!.removeEventListener(eventName, callback)
return () => ref.current?.removeEventListener(eventName, callback)
}, [ref, eventName, callback])
}