AccountGallery: fix crash in React 18

Fixes https://gitlab.com/soapbox-pub/soapbox/-/issues/1318
This commit is contained in:
Alex Gleason 2023-01-12 09:41:11 -06:00
parent c6700539f7
commit 8cbdc6019f
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect, useLayoutEffect, useRef, useState } from 'react';
import { FormattedMessage } from 'react-intl';
import { useParams } from 'react-router-dom';
@ -66,10 +66,7 @@ const AccountGallery = () => {
const hasMore = useAppSelector((state) => state.timelines.get(`account:${accountId}:media`)?.hasMore);
const [width, setWidth] = useState(323);
const handleRef = (c: HTMLDivElement) => {
if (c) setWidth(c.offsetWidth);
};
const node = useRef<HTMLDivElement>(null);
const handleScrollToBottom = () => {
if (hasMore) {
@ -99,6 +96,12 @@ const AccountGallery = () => {
}
};
useLayoutEffect(() => {
if (node.current) {
setWidth(node.current.offsetWidth);
}
}, [node.current]);
useEffect(() => {
if (accountId && accountId !== -1) {
dispatch(fetchAccount(accountId));
@ -140,7 +143,7 @@ const AccountGallery = () => {
return (
<Column label={`@${accountUsername}`} transparent withHeader={false}>
<div role='feed' className='account-gallery__container' ref={handleRef}>
<div role='feed' className='account-gallery__container' ref={node}>
{attachments.map((attachment, index) => attachment === null ? (
<LoadMoreMedia key={'more:' + attachments.get(index + 1)?.id} maxId={index > 0 ? (attachments.get(index - 1)?.id || null) : null} onLoadMore={handleLoadMore} />
) : (