Optimize memo of Stream.tsx

The Stream only has to re-render when 'stream.length' changes, but
could be re-rendered by a parent in scenarios where 'stream.length'
has not changed. This change optimizes that scenario by only
re-rendering the stream when 'stream.length' changes.
This commit is contained in:
0x1eef 2023-12-04 01:59:13 -03:00
parent 1a7af1be2a
commit 46850a8cfb

View file

@ -26,9 +26,11 @@ export function Stream({
const className = classNames("body", "stream");
const style: React.CSSProperties =
endOfStream || isPaused ? { overflowY: "auto" } : { overflowY: "hidden" };
const ul = useRef<HTMLUListElement>();
const ayat = useMemo<JSX.Element[]>(() => {
return stream.map((ayah: Quran.Ayah) => {
const ref = useRef<HTMLUListElement>();
const ul = useMemo<JSX.Element>(() => {
return (
<ul lang={locale} className={className} style={style} ref={ref}>
{stream.map((ayah: Quran.Ayah) => {
return (
<li key={ayah.id} className="ayah fade">
<span className="title">
@ -49,21 +51,19 @@ export function Stream({
<p>{ayah.text}</p>
</li>
);
});
})}
</ul>
);
}, [stream.length]);
useEffect(() => {
const el = ul.current;
const el = ref.current;
if (el) {
const top = el.scrollHeight + el.scrollTop;
el.scrollTo({ behavior: "smooth", top });
const top = 1024 + el.scrollHeight + el.scrollTop;
el.scrollBy({ behavior: "smooth", top });
el.scrollIntoView({ behavior: "smooth" });
}
}, [ul.current, stream.length]);
}, [stream.length]);
return (
<ul lang={locale} className={className} style={style} ref={ul}>
{ayat}
</ul>
);
return ul;
}