diff --git a/app/soapbox/features/chats/components/chat_message_list.tsx b/app/soapbox/features/chats/components/chat_message_list.tsx index a6719271a2..b6f4fcd29a 100644 --- a/app/soapbox/features/chats/components/chat_message_list.tsx +++ b/app/soapbox/features/chats/components/chat_message_list.tsx @@ -18,7 +18,7 @@ import DropdownMenuContainer from 'soapbox/containers/dropdown_menu_container'; import emojify from 'soapbox/features/emoji/emoji'; import Bundle from 'soapbox/features/ui/components/bundle'; import { MediaGallery } from 'soapbox/features/ui/util/async-components'; -import { useAppSelector, useAppDispatch } from 'soapbox/hooks'; +import { useAppSelector, useAppDispatch, useRefEventHandler } from 'soapbox/hooks'; import { onlyEmoji } from 'soapbox/utils/rich_content'; import type { Menu } from 'soapbox/components/dropdown_menu'; @@ -134,12 +134,12 @@ const ChatMessageList: React.FC = ({ chatId, chatMessageIds }) useEffect(() => { dispatch(fetchChatMessages(chatId)); - node.current?.addEventListener('scroll', handleScroll); + node.current?.addEventListener('scroll', e => handleScroll.current(e)); window.addEventListener('resize', handleResize); scrollToBottom(); return () => { - node.current?.removeEventListener('scroll', handleScroll); + node.current?.removeEventListener('scroll', e => handleScroll.current(e)); window.removeEventListener('resize', handleResize); }; }, []); @@ -190,7 +190,7 @@ const ChatMessageList: React.FC = ({ chatId, chatMessageIds }) setIsLoading(true); }; - const handleScroll = throttle(() => { + const handleScroll = useRefEventHandler(throttle(() => { if (node.current) { const { scrollTop, offsetHeight } = node.current; const computedScroll = lastComputedScroll.current === scrollTop; @@ -202,7 +202,7 @@ const ChatMessageList: React.FC = ({ chatId, chatMessageIds }) } }, 150, { trailing: true, - }); + })); const onOpenMedia = (media: any, index: number) => { dispatch(openModal('MEDIA', { media, index })); diff --git a/app/soapbox/hooks/index.ts b/app/soapbox/hooks/index.ts index 66fd310596..c25cb082a0 100644 --- a/app/soapbox/hooks/index.ts +++ b/app/soapbox/hooks/index.ts @@ -4,6 +4,7 @@ export { useAppSelector } from './useAppSelector'; export { useFeatures } from './useFeatures'; export { useOnScreen } from './useOnScreen'; export { useOwnAccount } from './useOwnAccount'; +export { useRefEventHandler } from './useRefEventHandler'; export { useSettings } from './useSettings'; export { useSoapboxConfig } from './useSoapboxConfig'; export { useSystemTheme } from './useSystemTheme'; diff --git a/app/soapbox/hooks/useRefEventHandler.ts b/app/soapbox/hooks/useRefEventHandler.ts new file mode 100644 index 0000000000..503a126e5c --- /dev/null +++ b/app/soapbox/hooks/useRefEventHandler.ts @@ -0,0 +1,9 @@ +import { useRef } from 'react'; + +/** Hook that allows using useState values in event handlers. */ +// https://stackoverflow.com/a/64770671/8811886 +export const useRefEventHandler = (fn: (...params: any) => void) => { + const ref = useRef(fn); + ref.current = fn; + return ref; +};