Skip to content

Commit 81f33ba

Browse files
committed
fix: do not rerender on client options update (#2465)
πŸš‚ #2463 It might be better not to recreate the client every time options update, since `useCreateChatClient({ options: { /* some inline options */ } })` will probably be a popular way to use this hook.
1 parent ba38eeb commit 81f33ba

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

β€Ždocusaurus/docs/React/basics/getting-started.mdxβ€Ž

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -147,34 +147,28 @@ body,
147147
To communicate with the Stream Chat API the SDK requires a client with an established connection. The hook mentioned in the code above (`useCreateChatClient`) handles client instantiation, establishes proper connection and handles cleanups and disconnects for you. If you wish to have more control over how all the previously mentioned is being handled see [Client and User](../guides/client-and-user.mdx) guide.
148148

149149
:::important
150-
The hook `useCreateChatClient` accepts parameter `options`. This is an object forwarded to the `StreamChat` constructor. Please make sure the `options` object is created outside the scope of the component invoking `useCreateChatClient` to prevent unnecessary re-renders and thus reconnects.
150+
The hook `useCreateChatClient` accepts parameter `options`. This is an object forwarded to the `StreamChat` constructor. When the client is created, the first passed `options` value is used, and the client is **not** recreated when the `options` value updates. In most cases it's not a problem, however if you really need to recreate the client with the latest options and reconnect, you can set a `key` on the component that invokes `useCreateChatClient`:
151151

152-
```
153-
import {
154-
Chat,
155-
StreamChatOptions,
156-
useCreateChatClient,
157-
} from 'stream-chat-react';
158-
159-
const streamChatOptions: StreamChatOptions = {
160-
timeout: 6000
161-
}
152+
```ts
153+
import { Chat, StreamChatOptions, useCreateChatClient } from 'stream-chat-react';
162154

163155
const App = () => {
156+
const [timeout, setTimeout] = useState(6000);
157+
const key = `timeout_${timeout}`;
158+
return <ChatWithOptions key={key} timeout={timeout} />;
159+
};
160+
161+
const ChatWithOptions = ({ timeout }: StreamChatOptions) => {
164162
const client = useCreateChatClient({
165163
apiKey,
166-
options: streamChatOptions,
164+
options: { timeout },
167165
tokenOrProvider: token,
168166
userData: { id: userId },
169167
});
170168

171169
if (!client) return <div>Loading...</div>;
172-
173-
return (
174-
<Chat client={client}>
175-
</Chat>
176-
);
177-
}
170+
return <Chat client={client}></Chat>;
171+
};
178172
```
179173

180174
:::

β€Žsrc/components/Chat/hooks/useCreateChatClient.tsβ€Ž

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ export const useCreateChatClient = <SCG extends ExtendableGenerics = DefaultGene
3232
setCachedUserData(userData);
3333
}
3434

35+
const [cachedOptions] = useState(options);
36+
3537
useEffect(() => {
36-
const client = new StreamChat<SCG>(apiKey, undefined, options);
38+
const client = new StreamChat<SCG>(apiKey, undefined, cachedOptions);
3739
let didUserConnectInterrupt = false;
3840

3941
const connectionPromise = client.connectUser(cachedUserData, tokenOrProvider).then(() => {
@@ -49,7 +51,7 @@ export const useCreateChatClient = <SCG extends ExtendableGenerics = DefaultGene
4951
console.log(`Connection for user "${cachedUserData.id}" has been closed`);
5052
});
5153
};
52-
}, [apiKey, cachedUserData, options, tokenOrProvider]);
54+
}, [apiKey, cachedUserData, cachedOptions, tokenOrProvider]);
5355

5456
return chatClient;
5557
};

0 commit comments

Comments
Β (0)