Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly resolve sendMessage during memoization #2675

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion package/src/components/Channel/Channel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2219,6 +2219,17 @@ const ChannelWithContext = <
watchers,
});

// This is mainly a hack to get around an issue with sendMessage not being passed correctly as a
// useMemo() dependency. The easy fix is to add it to the dependency array, however that would mean
// that this (very used) context is essentially going to cause rerenders on pretty much every Channel
// render, since sendMessage is an inline function. Wrapping it in useCallback() is one way to fix it
// but it is definitely not trivial, especially considering it depends on other inline functions that
// are not wrapped in a useCallback() themselves hence creating a huge cascading change. Can be removed
// once our memoization issues are fixed in most places in the app or we move to a reactive state store.
const sendMessageRef =
useRef<InputMessageInputContextValue<StreamChatGenerics>['sendMessage']>(sendMessage);
sendMessageRef.current = sendMessage;

const inputMessageInputContext = useCreateInputMessageInputContext<StreamChatGenerics>({
additionalTextInputProps,
asyncMessagesLockDistance,
Expand Down Expand Up @@ -2269,7 +2280,7 @@ const ChannelWithContext = <
quotedMessage,
SendButton,
sendImageAsync,
sendMessage,
sendMessage: (...args) => sendMessageRef.current(...args),
SendMessageDisallowedIndicator,
setInputRef,
setQuotedMessageState,
Expand Down
Loading