nova-betterchat/src/hooks/useSaveToLocalStorage.ts

20 lines
516 B
TypeScript
Raw Normal View History

import React, { useEffect, useRef } from 'react';
2023-03-03 23:17:11 +01:00
import useStore from '@store/store';
const useSaveToLocalStorage = () => {
const chatsRef = useRef(useStore.getState().chats);
2023-03-03 23:17:11 +01:00
useEffect(() => {
const unsubscribe = useStore.subscribe((state) => {
if (chatsRef && chatsRef.current !== state.chats) {
chatsRef.current = state.chats;
localStorage.setItem('chats', JSON.stringify(state.chats));
}
});
return unsubscribe;
}, []);
2023-03-03 23:17:11 +01:00
};
export default useSaveToLocalStorage;