Skip to content

Commit 7e7a1b3

Browse files
committed
fix: make sure to use latest userData of both user & apiKey/token change together
1 parent caf13c5 commit 7e7a1b3

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

package/src/components/Chat/hooks/useCreateChatClient.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ export const useCreateChatClient = ({
2424
options?: StreamChatOptions;
2525
}) => {
2626
const [chatClient, setChatClient] = useState<StreamChat | null>(null);
27-
const [cachedUserData, setCachedUserData] = useState(userData);
2827

29-
if (userData.id !== cachedUserData.id) {
30-
setCachedUserData(userData);
31-
}
28+
const userDataRef = useRef(userData);
29+
userDataRef.current = userData;
3230

3331
const optionsRef = useRef(options);
3432
optionsRef.current = options;
@@ -37,7 +35,7 @@ export const useCreateChatClient = ({
3735
const client = new StreamChat(apiKey, undefined, optionsRef.current);
3836
let didUserConnectInterrupt = false;
3937

40-
const connectionPromise = client.connectUser(cachedUserData, tokenOrProvider).then(() => {
38+
const connectionPromise = client.connectUser(userDataRef.current, tokenOrProvider).then(() => {
4139
if (!didUserConnectInterrupt) {
4240
setChatClient(client);
4341
}
@@ -49,10 +47,11 @@ export const useCreateChatClient = ({
4947
connectionPromise
5048
.then(() => client.disconnectUser())
5149
.then(() => {
52-
console.log(`Connection for user "${cachedUserData.id}" has been closed`);
50+
console.log(`Connection for user "${userDataRef.current.id}" has been closed`);
5351
});
5452
};
55-
}, [apiKey, cachedUserData, tokenOrProvider]);
53+
// we should recompute the client on user id change
54+
}, [apiKey, userData.id, tokenOrProvider]);
5655

5756
return chatClient;
5857
};

0 commit comments

Comments
 (0)