Skip to content

Commit

Permalink
fix: forward StreamChat constructor options via useCreateChatClient
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCupela committed Aug 5, 2024
1 parent 43d1540 commit fff18e4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 5 additions & 1 deletion docusaurus/docs/React/basics/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ body,

## Chat Client & Connecting User

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 of the previously mentioned is being handled see [Client and User](../guides/client-and-user.mdx) guide.
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.

:::important
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.
:::

## Creating a Channel

Expand Down
12 changes: 8 additions & 4 deletions src/components/Chat/hooks/useCreateChatClient.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useEffect, useState } from 'react';

import {
import { StreamChat } from 'stream-chat';

import type {
DefaultGenerics,
ExtendableGenerics,
OwnUserResponse,
StreamChat,
StreamChatOptions,
TokenOrProvider,
UserResponse,
} from 'stream-chat';
Expand All @@ -14,12 +16,14 @@ import {
*/
export const useCreateChatClient = <SCG extends ExtendableGenerics = DefaultGenerics>({
apiKey,
options,

Check warning on line 19 in src/components/Chat/hooks/useCreateChatClient.ts

View check run for this annotation

Codecov / codecov/patch

src/components/Chat/hooks/useCreateChatClient.ts#L19

Added line #L19 was not covered by tests
tokenOrProvider,
userData,
}: {
apiKey: string;
tokenOrProvider: TokenOrProvider;
userData: OwnUserResponse<SCG> | UserResponse<SCG>;
options?: StreamChatOptions;
}) => {
const [chatClient, setChatClient] = useState<StreamChat<SCG> | null>(null);
const [cachedUserData, setCachedUserData] = useState(userData);
Expand All @@ -29,7 +33,7 @@ export const useCreateChatClient = <SCG extends ExtendableGenerics = DefaultGene
}

useEffect(() => {
const client = new StreamChat<SCG>(apiKey);
const client = new StreamChat<SCG>(apiKey, undefined, options);

Check warning on line 36 in src/components/Chat/hooks/useCreateChatClient.ts

View check run for this annotation

Codecov / codecov/patch

src/components/Chat/hooks/useCreateChatClient.ts#L36

Added line #L36 was not covered by tests
let didUserConnectInterrupt = false;

const connectionPromise = client.connectUser(cachedUserData, tokenOrProvider).then(() => {
Expand All @@ -45,7 +49,7 @@ export const useCreateChatClient = <SCG extends ExtendableGenerics = DefaultGene
console.log(`Connection for user "${cachedUserData.id}" has been closed`);
});
};
}, [apiKey, cachedUserData, tokenOrProvider]);
}, [apiKey, cachedUserData, options, tokenOrProvider]);

return chatClient;
};

0 comments on commit fff18e4

Please sign in to comment.