Skip to content

Commit f6b5a6b

Browse files
committed
fix: memoize addNotification function provided by Channel component
1 parent e6bfd40 commit f6b5a6b

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/components/Channel/Channel.tsx

+7-4
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ const ChannelInner = <
407407
const [quotedMessage, setQuotedMessage] = useState<StreamMessage<StreamChatGenerics>>();
408408
const [channelUnreadUiState, _setChannelUnreadUiState] = useState<ChannelUnreadUiState>();
409409

410-
const notificationTimeouts: Array<NodeJS.Timeout> = [];
410+
const notificationTimeouts = useRef<Array<NodeJS.Timeout>>([]);
411411

412412
const [state, dispatch] = useReducer<ChannelStateReducer<StreamChatGenerics>>(
413413
channelReducer,
@@ -641,15 +641,15 @@ const ChannelInner = <
641641
channel.on(handleEvent);
642642
}
643643
})();
644+
const notificationTimeoutsRef = notificationTimeouts.current;
644645

645646
return () => {
646647
if (errored || !done) return;
647648
channel?.off(handleEvent);
648649
client.off('connection.changed', handleEvent);
649650
client.off('connection.recovered', handleEvent);
650-
client.off('user.updated', handleEvent);
651651
client.off('user.deleted', handleEvent);
652-
notificationTimeouts.forEach(clearTimeout);
652+
notificationTimeoutsRef.forEach(clearTimeout);
653653
};
654654
// eslint-disable-next-line react-hooks/exhaustive-deps
655655
}, [
@@ -671,7 +671,10 @@ const ChannelInner = <
671671
/** MESSAGE */
672672

673673
// Adds a temporary notification to message list, will be removed after 5 seconds
674-
const addNotification = makeAddNotifications(setNotifications, notificationTimeouts);
674+
const addNotification = useMemo(
675+
() => makeAddNotifications(setNotifications, notificationTimeouts.current),
676+
[],
677+
);
675678

676679
// eslint-disable-next-line react-hooks/exhaustive-deps
677680
const loadMoreFinished = useCallback(

0 commit comments

Comments
 (0)