From 14457cbb5fe70806d83eeb82d93852817e23073a Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Fri, 17 Mar 2023 10:42:16 +0800 Subject: [PATCH] fix chatstore state --- src/app.tsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/app.tsx b/src/app.tsx index 28f90b6..2848391 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -74,14 +74,20 @@ export function App() { return JSON.parse(val) as ChatStore; }; - const chatStore = getChatStoreByIndex(selectedChatIndex); + const [chatStore, _setChatStore] = useState( + getChatStoreByIndex(selectedChatIndex) + ); const setChatStore = (cs: ChatStore) => { console.log("saved chat", selectedChatIndex, chatStore); localStorage.setItem( `${STORAGE_NAME}-${selectedChatIndex}`, JSON.stringify(cs) ); + _setChatStore(cs); }; + useEffect(() => { + _setChatStore(getChatStoreByIndex(selectedChatIndex)); + }, [selectedChatIndex]); return (