Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: forward StreamChat constructor options via useCreateChatClient #2463

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
MartinCupela marked this conversation as resolved.
Show resolved Hide resolved
:::

## 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 @@
*/
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 @@
}

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
MartinCupela marked this conversation as resolved.
Show resolved Hide resolved
let didUserConnectInterrupt = false;

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

return chatClient;
};
Loading