@@ -68,7 +68,7 @@ const Swiper = forwardRefWithStatics(
6868 const rootDiv = useRef < HTMLDivElement > ( null ) ; // 根节点
6969 const swiperContainer = useRef < HTMLDivElement > ( null ) ; // swiper容器节点
7070 const swiperSource = useRef < SwiperChangeSource > ( 'autoplay' ) ; // swiper变化来源
71- const previousIndex = useRef ( current || defaultCurrent || 0 ) ; // 上一次轮播页索引
71+ const previousIndex = useRef < number > ( current ?? defaultCurrent ?? 0 ) ; // 上一次轮播页索引
7272 const nextIndex = useRef ( previousIndex . current ) ;
7373 const items = useRef < SwiperItemReference [ ] > ( [ ] ) ; // swiper子项
7474 const [ itemCount , setItemCount ] = useState ( 0 ) ; // 轮播子项数量
@@ -417,7 +417,8 @@ const Swiper = forwardRefWithStatics(
417417 } ) ;
418418
419419 useEffect ( ( ) => {
420- // 初始化卡片的位置
420+ if ( ! items . current . length ) return ;
421+
421422 previousIndex . current = calculateItemIndex ( previousIndex . current , items . current . length , loop ) ;
422423 updateSwiperItemClassName ( previousIndex . current , loop ) ;
423424 setDotIndex ( ( ) => previousIndex . current ) ;
@@ -426,11 +427,12 @@ const Swiper = forwardRefWithStatics(
426427
427428 useEffect ( ( ) => {
428429 if ( currentIsNull ) return ;
430+ if ( ! items . current . length ) return ;
429431 nextIndex . current = calculateItemIndex ( current , items . current . length , loop ) ;
430432 if ( previousIndex . current !== nextIndex . current ) {
431433 enterSwitching ( directionAxis ) ;
432434 }
433- } , [ calculateItemIndex , current , directionAxis , enterSwitching , loop , currentIsNull ] ) ;
435+ } , [ calculateItemIndex , current , directionAxis , enterSwitching , loop , currentIsNull , itemCount ] ) ;
434436
435437 useEffect ( ( ) => {
436438 onChange ?.( previousIndex . current , { source : swiperSource . current } ) ;
0 commit comments