Skip to content

Commit 66d4e73

Browse files
committed
Update to use ref to chatbox
1 parent 4858ed8 commit 66d4e73

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

react/remote-work-demo/src/components/TeamChat.jsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useCallback } from "react";
1+
import { useState, useCallback, useRef } from "react";
22
import Talk from "talkjs";
33
import { Chatbox, useSession } from "@talkjs/react";
44
import talkJsConfig from "../talkJsConfig";
@@ -12,6 +12,7 @@ const TeamChat = ({ unreadMessages }) => {
1212
useState(initialConversation);
1313

1414
const session = useSession();
15+
const chatboxRef = useRef(null);
1516

1617
const [mobileChannelSelected, setMobileChannelSelected] = useState(true); //This is used to control whether or not to display the chatbox or the inbox while on mobile displays
1718

@@ -27,8 +28,9 @@ const TeamChat = ({ unreadMessages }) => {
2728
setMobileChannelSelected(true);
2829
setCurrentConversation(conversation);
2930

30-
const chatbox = session.getChatboxes()[0];
31-
chatbox.select(talkJsConversation);
31+
if (chatboxRef.current?.isAlive) {
32+
chatboxRef.current.select(talkJsConversation);
33+
}
3234
}
3335
};
3436

@@ -121,6 +123,7 @@ const TeamChat = ({ unreadMessages }) => {
121123
className="h-full w-full overflow-hidden rounded-b-xl lg:rounded-none lg:rounded-br-xl"
122124
showChatHeader={false}
123125
theme="team_chat"
126+
chatboxRef={chatboxRef}
124127
/>
125128
</div>
126129
</div>

0 commit comments

Comments
 (0)