From b3e1f1306e4b6a9ba20788167413138f3a4e4e9b Mon Sep 17 00:00:00 2001 From: Brecht Cloetens Date: Mon, 9 May 2022 15:55:50 +0200 Subject: [PATCH 1/2] Fixed undefined dom error. https://sentry.io/organizations/codeurs-bvba/issues/2877504533/?project=6071041&referrer=webhooks_plugin --- src/carousel.tsx | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/carousel.tsx b/src/carousel.tsx index 5efec40..851710b 100644 --- a/src/carousel.tsx +++ b/src/carousel.tsx @@ -38,7 +38,7 @@ type Snaps = {pages: Array; elements: Array} export const useCarousel = (options?: CarouselOptions) => { const [, gen] = useState(0) - const forceUpdate = useCallback(() => gen(x => x + 1), [gen]) + const forceUpdate = useCallback(() => gen((x) => x + 1), [gen]) const [current, setCurrent] = useState(0) const [total, setTotal] = useState(0) const target = useRef(0) @@ -63,7 +63,7 @@ export const useCarousel = (options?: CarouselOptions) => { const goPrevious = () => goTo(current - 1) return { current, - setCurrent: current => { + setCurrent: (current) => { setCurrent(current) forceUpdate() }, @@ -88,6 +88,7 @@ const mix = calc.getValueFromProgress const MY_RETINA_FIXING_MAGIC_NUMBER = 3 const calcSnaps = (dom: HTMLDivElement) => { + if (dom === null || typeof dom === 'undefined') return null const pageWidth = dom.offsetWidth const children = Array.from( (dom.firstChild! as HTMLDivElement).children @@ -173,6 +174,7 @@ export const Carousel: FunctionComponent< const update = useCallback((force?: boolean) => { const snaps = snapsRef.current const newSnaps = calcSnaps(dom.current!) + if (!newSnaps) return if (!force && snapKey(snaps) === snapKey(newSnaps)) return setTotal(Math.ceil(newSnaps.pages.length)) snapsRef.current = newSnaps @@ -260,10 +262,7 @@ export const Carousel: FunctionComponent< x: start, preventDefault: false }) - .pipe( - (pos: {x: number}) => pos.x, - overDrag - ) + .pipe((pos: {x: number}) => pos.x, overDrag) .start(offset).stop listen(document, 'mouseup touchend', {once: true}).start(() => snapToPoint(start) @@ -353,7 +352,7 @@ export const Carousel: FunctionComponent< >
e.preventDefault()} + onDragStart={(e) => e.preventDefault()} > {children}
From 6a2c079d2e340cb85ca8861fd3ed790f35017ca3 Mon Sep 17 00:00:00 2001 From: Brecht Cloetens Date: Mon, 9 May 2022 16:07:47 +0200 Subject: [PATCH 2/2] Simplified --- src/carousel.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/carousel.tsx b/src/carousel.tsx index 851710b..5f0d5e6 100644 --- a/src/carousel.tsx +++ b/src/carousel.tsx @@ -88,7 +88,7 @@ const mix = calc.getValueFromProgress const MY_RETINA_FIXING_MAGIC_NUMBER = 3 const calcSnaps = (dom: HTMLDivElement) => { - if (dom === null || typeof dom === 'undefined') return null + if (!dom) return null const pageWidth = dom.offsetWidth const children = Array.from( (dom.firstChild! as HTMLDivElement).children