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: do not rerender on client options update #2465

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 12 additions & 18 deletions docusaurus/docs/React/basics/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -174,34 +174,28 @@ body,
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.
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`:

```
import {
Chat,
StreamChatOptions,
useCreateChatClient,
} from 'stream-chat-react';

const streamChatOptions: StreamChatOptions = {
timeout: 6000
}
```ts
import { Chat, StreamChatOptions, useCreateChatClient } from 'stream-chat-react';

const App = () => {
const [timeout, setTimeout] = useState(6000);
const key = `timeout_${timeout}`;
return <ChatWithOptions key={key} timeout={timeout} />;
};

const ChatWithOptions = ({ timeout }: StreamChatOptions) => {
const client = useCreateChatClient({
apiKey,
options: streamChatOptions,
options: { timeout },
tokenOrProvider: token,
userData: { id: userId },
});

if (!client) return <div>Loading...</div>;

return (
<Chat client={client}>
</Chat>
);
}
return <Chat client={client}></Chat>;
};
```

:::
Expand Down
6 changes: 4 additions & 2 deletions src/components/Chat/hooks/useCreateChatClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
setCachedUserData(userData);
}

const [cachedOptions] = useState(options);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L35 was not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

return chatClient;
};
Loading