Skip to content

Commit 3899352

Browse files
authored
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 d1c7ffa commit 3899352

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
@@ -174,34 +174,28 @@ body,
174174
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.
175175

176176
:::important
177-
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.
177+
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`:
178178

179-
```
180-
import {
181-
Chat,
182-
StreamChatOptions,
183-
useCreateChatClient,
184-
} from 'stream-chat-react';
185-
186-
const streamChatOptions: StreamChatOptions = {
187-
timeout: 6000
188-
}
179+
```ts
180+
import { Chat, StreamChatOptions, useCreateChatClient } from 'stream-chat-react';
189181

190182
const App = () => {
183+
const [timeout, setTimeout] = useState(6000);
184+
const key = `timeout_${timeout}`;
185+
return <ChatWithOptions key={key} timeout={timeout} />;
186+
};
187+
188+
const ChatWithOptions = ({ timeout }: StreamChatOptions) => {
191189
const client = useCreateChatClient({
192190
apiKey,
193-
options: streamChatOptions,
191+
options: { timeout },
194192
tokenOrProvider: token,
195193
userData: { id: userId },
196194
});
197195

198196
if (!client) return <div>Loading...</div>;
199-
200-
return (
201-
<Chat client={client}>
202-
</Chat>
203-
);
204-
}
197+
return <Chat client={client}></Chat>;
198+
};
205199
```
206200

207201
:::

β€Ž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)