From 0910332f9a36ca4beffb9019c8d6db86d364c054 Mon Sep 17 00:00:00 2001 From: jinmiaoluo <39730824+jinmiaoluo@users.noreply.github.com> Date: Sat, 29 Apr 2023 13:37:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20revert=20=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E7=9A=84=E5=AE=9E=E7=8E=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 通过 ... 的语法创建一个新的 ChatSession[] 副本,在副本上执行 splice 操作,避免直接修改状态 --- app/store/chat.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/app/store/chat.ts b/app/store/chat.ts index 048dbe31e..b7e50b4c9 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -221,14 +221,16 @@ export const useChatStore = create()( { text: Locale.Home.Revert, onClick() { - const sessions = get().sessions; - // if the deleted session is the only session in sessions array, - // we should delete the empty session first before inserting the deleted session - sessions.splice(index, isLastSession, deletedSession); - set(() => ({ - sessions: sessions, - currentSessionIndex: seletedSessionIndex, - })); + set((state) => { + const newSessions = [...state.sessions]; + // if the deleted session is the only session in sessions array, + // we should delete the empty session first before inserting the deleted session + newSessions.splice(index, isLastSession, deletedSession); + return { + sessions: newSessions, + currentSessionIndex: seletedSessionIndex, + }; + }); }, }, 5000,