Skip to content

Commit 4c1a8c5

Browse files
only update state if needed
1 parent 9287b96 commit 4c1a8c5

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

dotcom-rendering/src/components/ScrollableCarousel.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { SerializedStyles } from '@emotion/react';
22
import { css } from '@emotion/react';
33
import type { Breakpoint } from '@guardian/source/foundations';
44
import { from, space, until } from '@guardian/source/foundations';
5-
import { useEffect, useRef, useState } from 'react';
5+
import { useCallback, useEffect, useRef, useState } from 'react';
66
import { nestedOphanComponents } from '../lib/ophan-helpers';
77
import { palette } from '../palette';
88
import { CarouselCount } from './CarouselCount';
@@ -358,7 +358,7 @@ export const ScrollableCarousel = ({
358358
* updates the state of cardCount. we use the half of a card because at
359359
* this scroll amount the carousel will snap to that card.
360360
*/
361-
const updateCardCountOnScroll = () => {
361+
const updateCardCountOnScroll = useCallback(() => {
362362
const carouselElement = carouselRef.current;
363363
if (!carouselElement) return;
364364
const cardWidth = carouselElement.querySelector('li')?.offsetWidth ?? 0;
@@ -368,7 +368,7 @@ export const ScrollableCarousel = ({
368368
if (count !== cardCount) {
369369
setCardCount(count);
370370
}
371-
};
371+
}, [cardCount]);
372372

373373
/**
374374
* Throttle scroll events to optimise performance. As we're only using this
@@ -474,7 +474,7 @@ export const ScrollableCarousel = ({
474474
throttleEvent(updateCardCountOnScroll),
475475
);
476476
};
477-
}, [isArticle]);
477+
}, [isArticle, updateCardCountOnScroll]);
478478

479479
return (
480480
<div css={[baseContainerStyles, !isArticle && frontContainerStyles]}>

0 commit comments

Comments
 (0)