diff --git a/package.json b/package.json index 35cdf36..c5fecc9 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "react-quill": "^2.0.0", "react-router-dom": "^6.8.1", "sass": "^1.58.1", - "swiper": "^9.0.5", + "swiper": "^11.1.4", "uuid": "^9.0.0", "vite": "^4.1.0", "vite-tsconfig-paths": "^4.0.5", diff --git a/src/hooks/useSwiperRef.ts b/src/hooks/useSwiperRef.ts new file mode 100644 index 0000000..f39a4c7 --- /dev/null +++ b/src/hooks/useSwiperRef.ts @@ -0,0 +1,14 @@ +import { useState, useRef, useEffect } from 'react'; + +const useSwiperRef = () => { + const [wrapper, setWrapper] = useState(null); + const ref = useRef(null); + + useEffect(() => { + setWrapper(ref.current); + }, []); + + return [wrapper, ref] as const; +}; + +export default useSwiperRef; diff --git a/src/pages/Introduce/index.tsx b/src/pages/Introduce/index.tsx index a43e504..cc8cb2a 100644 --- a/src/pages/Introduce/index.tsx +++ b/src/pages/Introduce/index.tsx @@ -2,7 +2,7 @@ import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { useQuery } from '@tanstack/react-query'; import { motion } from 'framer-motion'; -import { Scrollbar } from 'swiper'; +import { Scrollbar } from 'swiper/modules'; import { Swiper, SwiperSlide } from 'swiper/react'; import ReviewBoard from '@/components/Introduce/ReviewBoard'; import TrendBoard from '@/components/Introduce/TrendBoard'; diff --git a/src/pages/Main/index.tsx b/src/pages/Main/index.tsx index 8acd749..e1abe5b 100644 --- a/src/pages/Main/index.tsx +++ b/src/pages/Main/index.tsx @@ -1,17 +1,18 @@ import 'swiper/css'; import 'swiper/css/pagination'; import 'swiper/css/navigation'; -import { useEffect, useRef, useState } from 'react'; +import { useState } from 'react'; import { Link, useNavigate } from 'react-router-dom'; import { useQuery } from '@tanstack/react-query'; import dayjs from 'dayjs'; import { motion } from 'framer-motion'; -import { Autoplay, Pagination, Navigation } from 'swiper'; +import { Autoplay, Pagination, Navigation } from 'swiper/modules'; import { Swiper, SwiperSlide } from 'swiper/react'; import footer from '@/assets/common/footer.png'; import { QueryKeys, restFetcher } from '@/queryClient'; import { CommunityBoardType } from '@/types/Board/communityType'; import { IntroBoardType } from '@/types/Board/introType'; +import useSwiperRef from '@/hooks/useSwiperRef'; import { getPrefixCategoryName } from '@/utils/utils'; import { ApiResponseWithDataType } from '@/types/apiResponseType'; import { jumbotronData } from '@/constants/main_dummy'; @@ -19,13 +20,14 @@ import { opacityVariants } from '@/constants/variants'; import styles from './styles.module.scss'; export default function MainPage() { - const [_, updateState] = useState(false); const [introToggle, setIntroToggle] = useState<'trend' | 'review'>('trend'); const navigate = useNavigate(); - const introNextRef = useRef(null); - const introPrevRef = useRef(null); - const commuNextRef = useRef(null); - const commuPrevRef = useRef(null); + + const [introPrevEl, introPrevRef] = useSwiperRef(); + const [introNextEl, introNextRef] = useSwiperRef(); + const [commuNextEl, commuNextRef] = useSwiperRef(); + const [commuPrevEl, commuPrevRef] = useSwiperRef(); + const { data: introData } = useQuery< ApiResponseWithDataType >([QueryKeys.PREVIEW_BOARD, QueryKeys.INTRO_BOARD, introToggle], () => @@ -52,10 +54,6 @@ export default function MainPage() { }), ); - useEffect(() => { - updateState(true); - }, [introNextRef, commuNextRef]); - return ( {jumbotronData.map((data, idx) => ( @@ -151,8 +149,8 @@ export default function MainPage() { (null); + + const [paginationEl, paginationRef] = useSwiperRef(); const handleDeleteButtonClick = async (houseId: number) => { if (houseId === 0) throw new Error('없는 농가거래 게시물입니다.'); @@ -160,7 +162,7 @@ export default function TradeBoardPage() { spaceBetween={50} slidesPerView={1} navigation - pagination={{ el: ref.current }} + pagination={{ el: paginationEl }} scrollbar={{ draggable: true }} > {data?.data.imageUrls.map((url, index) => ( @@ -169,7 +171,7 @@ export default function TradeBoardPage() { ))} -
+