diff --git a/packages/react/src/chat/provider.tsx b/packages/react/src/chat/provider.tsx index c96c216a..93c28c75 100644 --- a/packages/react/src/chat/provider.tsx +++ b/packages/react/src/chat/provider.tsx @@ -24,28 +24,37 @@ export function ChatProvider(props: ChatProviderProps): JSX.Element { headers, } = props; - const store = useRef(null); + const store = useRef<{ + storeKey: string | undefined; + store: ChatStore | null; + }>({ + storeKey: undefined, + store: null, + }); - if (!store.current) { - store.current = createChatStore({ - apiUrl, - headers, - projectKey, - chatOptions, - debug, - persistChatHistory: chatOptions?.history, + if (!store.current?.store || store.current?.storeKey !== storeKey) { + store.current = { storeKey, - }); + store: createChatStore({ + apiUrl, + headers, + projectKey, + chatOptions, + debug, + persistChatHistory: chatOptions?.history, + storeKey, + }), + }; } // update chat options when they change useEffect(() => { if (!chatOptions) return; - store.current?.getState().setOptions(chatOptions); + store.current?.store?.getState().setOptions(chatOptions); }, [chatOptions]); return ( - + {children} );