Skip to content

Commit 51893bb

Browse files
authored
fix: state break on channel prop update (#2957)
* fix: Channel and MessageList state breaking completely if the channel prop gets updated * fix: broken hook deps * chore: remove redundant log
1 parent b117342 commit 51893bb

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

package/src/components/Channel/Channel.tsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { PropsWithChildren, useCallback, useEffect, useRef, useState } from 'react';
1+
import React, { PropsWithChildren, useCallback, useEffect, useMemo, useRef, useState } from 'react';
22
import { KeyboardAvoidingViewProps, StyleSheet, Text, View } from 'react-native';
33

44
import debounce from 'lodash/debounce';
@@ -749,18 +749,20 @@ const ChannelWithContext = <
749749
? newMessageStateUpdateThrottleInterval
750750
: stateUpdateThrottleInterval;
751751

752-
const copyChannelState = useRef(
753-
throttle(
754-
() => {
755-
if (channel) {
756-
copyStateFromChannel(channel);
757-
copyMessagesStateFromChannel(channel);
758-
}
759-
},
760-
copyChannelStateThrottlingTime,
761-
throttleOptions,
762-
),
763-
).current;
752+
const copyChannelState = useMemo(
753+
() =>
754+
throttle(
755+
() => {
756+
if (channel) {
757+
copyStateFromChannel(channel);
758+
copyMessagesStateFromChannel(channel);
759+
}
760+
},
761+
copyChannelStateThrottlingTime,
762+
throttleOptions,
763+
),
764+
[channel, copyChannelStateThrottlingTime, copyMessagesStateFromChannel, copyStateFromChannel],
765+
);
764766

765767
const handleEvent: EventHandler<StreamChatGenerics> = (event) => {
766768
if (shouldSyncChannel) {

package/src/components/MessageList/MessageList.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ const MessageListWithContext = <
579579
}
580580
}
581581
// eslint-disable-next-line react-hooks/exhaustive-deps
582-
}, [rawMessageList, threadList]);
582+
}, [channel, rawMessageList, threadList]);
583583

584584
const goToMessage = async (messageId: string) => {
585585
const indexOfParentInMessageList = processedMessageList.findIndex(
@@ -1097,12 +1097,13 @@ const MessageListWithContext = <
10971097
keyboardShouldPersistTaps='handled'
10981098
keyExtractor={keyExtractor}
10991099
ListFooterComponent={ListFooterComponent}
1100+
ListHeaderComponent={ListHeaderComponent}
11001101
/**
1101-
if autoscrollToTopThreshold is 10, we scroll to recent if before new list update it was already at the bottom (10 offset or below)
1102-
minIndexForVisible = 1 means that beyond item at index 1 will not change position on list updates
1103-
minIndexForVisible is not used when autoscrollToTopThreshold = 10
1102+
If autoscrollToTopThreshold is 10, we scroll to recent only if before the update, the list was already at the
1103+
bottom (10 offset or below).
1104+
minIndexForVisible = 1 means that beyond the item at index 1 we will not change the position on list updates,
1105+
however it is not used when autoscrollToTopThreshold = 10.
11041106
*/
1105-
ListHeaderComponent={ListHeaderComponent}
11061107
maintainVisibleContentPosition={{
11071108
autoscrollToTopThreshold: autoscrollToRecent ? 10 : undefined,
11081109
minIndexForVisible: 1,

package/src/components/MessageList/hooks/useShouldScrollToRecentOnNewOwnMessage.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ export function useShouldScrollToRecentOnNewOwnMessage<
3838
}
3939
}
4040
}
41-
// eslint-disable-next-line react-hooks/exhaustive-deps
42-
}, [rawMessageList]);
41+
}, [currentUserId, rawMessageList]);
4342

4443
return isMyOwnNewMessageRef;
4544
}

0 commit comments

Comments
 (0)