Skip to content

Commit

Permalink
fix: memoize addNotification function provided by Channel component (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCupela committed Jun 14, 2024
1 parent cb09dc1 commit b3734a4
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/components/Channel/Channel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ const ChannelInner = <
const [quotedMessage, setQuotedMessage] = useState<StreamMessage<StreamChatGenerics>>();
const [channelUnreadUiState, _setChannelUnreadUiState] = useState<ChannelUnreadUiState>();

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

const [state, dispatch] = useReducer<ChannelStateReducer<StreamChatGenerics>>(
channelReducer,
Expand Down Expand Up @@ -641,15 +641,15 @@ const ChannelInner = <
channel.on(handleEvent);
}
})();
const notificationTimeoutsRef = notificationTimeouts.current;

return () => {
if (errored || !done) return;
channel?.off(handleEvent);
client.off('connection.changed', handleEvent);
client.off('connection.recovered', handleEvent);
client.off('user.updated', handleEvent);
client.off('user.deleted', handleEvent);
notificationTimeouts.forEach(clearTimeout);
notificationTimeoutsRef.forEach(clearTimeout);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
Expand All @@ -671,7 +671,10 @@ const ChannelInner = <
/** MESSAGE */

// Adds a temporary notification to message list, will be removed after 5 seconds
const addNotification = makeAddNotifications(setNotifications, notificationTimeouts);
const addNotification = useMemo(
() => makeAddNotifications(setNotifications, notificationTimeouts.current),
[],
);

// eslint-disable-next-line react-hooks/exhaustive-deps
const loadMoreFinished = useCallback(
Expand Down

0 comments on commit b3734a4

Please sign in to comment.