Skip to content

Commit fcb363e

Browse files
authored
Merge pull request #34 from codeurs/undefined-fix
Fixed undefined dom error.
2 parents cad9a08 + 6a2c079 commit fcb363e

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/carousel.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type Snaps = {pages: Array<number>; elements: Array<number>}
3838

3939
export const useCarousel = (options?: CarouselOptions) => {
4040
const [, gen] = useState(0)
41-
const forceUpdate = useCallback(() => gen(x => x + 1), [gen])
41+
const forceUpdate = useCallback(() => gen((x) => x + 1), [gen])
4242
const [current, setCurrent] = useState(0)
4343
const [total, setTotal] = useState(0)
4444
const target = useRef(0)
@@ -63,7 +63,7 @@ export const useCarousel = (options?: CarouselOptions) => {
6363
const goPrevious = () => goTo(current - 1)
6464
return {
6565
current,
66-
setCurrent: current => {
66+
setCurrent: (current) => {
6767
setCurrent(current)
6868
forceUpdate()
6969
},
@@ -88,6 +88,7 @@ const mix = calc.getValueFromProgress
8888
const MY_RETINA_FIXING_MAGIC_NUMBER = 3
8989

9090
const calcSnaps = (dom: HTMLDivElement) => {
91+
if (!dom) return null
9192
const pageWidth = dom.offsetWidth
9293
const children = Array.from(
9394
(dom.firstChild! as HTMLDivElement).children
@@ -173,6 +174,7 @@ export const Carousel: FunctionComponent<
173174
const update = useCallback((force?: boolean) => {
174175
const snaps = snapsRef.current
175176
const newSnaps = calcSnaps(dom.current!)
177+
if (!newSnaps) return
176178
if (!force && snapKey(snaps) === snapKey(newSnaps)) return
177179
setTotal(Math.ceil(newSnaps.pages.length))
178180
snapsRef.current = newSnaps
@@ -260,10 +262,7 @@ export const Carousel: FunctionComponent<
260262
x: start,
261263
preventDefault: false
262264
})
263-
.pipe(
264-
(pos: {x: number}) => pos.x,
265-
overDrag
266-
)
265+
.pipe((pos: {x: number}) => pos.x, overDrag)
267266
.start(offset).stop
268267
listen(document, 'mouseup touchend', {once: true}).start(() =>
269268
snapToPoint(start)
@@ -353,7 +352,7 @@ export const Carousel: FunctionComponent<
353352
>
354353
<div
355354
style={{height: '100%', display: 'flex'}}
356-
onDragStart={e => e.preventDefault()}
355+
onDragStart={(e) => e.preventDefault()}
357356
>
358357
{children}
359358
</div>

0 commit comments

Comments
 (0)