Skip to content

Commit ce69ebc

Browse files
authored
fix: handle scrollRef.current == null case (#516)
### Description Of Changes Resolves [UIKIT-3790](https://sendbird.atlassian.net/browse/UIKIT-3790) scrollRef.current could be null and it caused a runtime error. So added a safe guard for it.
1 parent ea213f3 commit ce69ebc

File tree

2 files changed

+13
-8
lines changed
  • src
    • hooks/useHandleOnScrollCallback
    • modules/Channel/components/MessageList

2 files changed

+13
-8
lines changed

src/hooks/useHandleOnScrollCallback/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ export function useHandleOnScrollCallback({
1919
setShowScrollDownButton,
2020
}: UseHandleOnScrollCallbackProps): () => void {
2121
return useCallback(() => {
22-
const element = scrollRef.current;
22+
const element = scrollRef?.current;
23+
if (element == null) {
24+
return;
25+
}
26+
2327
const {
2428
scrollTop,
2529
scrollHeight,
@@ -40,7 +44,7 @@ export function useHandleOnScrollCallback({
4044
if (scrollTop < SCROLL_BUFFER) {
4145
onScroll(() => {
4246
// sets the scroll position to the bottom of the new messages
43-
scrollRef.current.scrollTop = scrollRef.current.scrollHeight - scrollBottom;
47+
element.scrollTop = element.scrollHeight - scrollBottom;
4448
});
4549
}
4650
}, [

src/modules/Channel/components/MessageList/index.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ const MessageList: React.FC<MessageListProps> = ({
5858
: allMessages;
5959
const markAsReadScheduler = store.config.markAsReadScheduler;
6060

61-
const onScroll = (e) => {
62-
const element = e.target;
61+
const onScroll = () => {
62+
const element = scrollRef?.current;
63+
if (element == null) {
64+
return;
65+
}
66+
6367
const {
6468
scrollTop,
6569
clientHeight,
@@ -80,10 +84,7 @@ const MessageList: React.FC<MessageListProps> = ({
8084
}
8185

8286
// Save the lastest scroll bottom value
83-
if (scrollRef?.current) {
84-
const current = scrollRef?.current;
85-
setScrollBottom(current.scrollHeight - current.scrollTop - current.offsetHeight)
86-
}
87+
setScrollBottom(element.scrollHeight - element.scrollTop - element.offsetHeight)
8788

8889
if (!disableMarkAsRead && isAboutSame(clientHeight + scrollTop, scrollHeight, SCROLL_BUFFER)) {
8990
messagesDispatcher({

0 commit comments

Comments
 (0)