728x90
๋ฐ์ํ
nextjs12 ๋ฅผ ์ฐ๋ค๊ฐ nextjs14๋ก ๊ธ ์ ํ๋ฅผ ํ๋ค๋ณด๋ ์๋ก์ด ๊ฒ๋ค์ด ๊ฝค ๋์ ๋ค์ด์๋ค. ํด๋น ๋ด์ฉ์ nextjs13 ์ดํ์๋ ์ ์ฉ์ด ๋๋ ๋ด์ฉ์ด๋ค. ํ์ด์ง๋ฅผ ๋ฆฌ๋ก๋ํ๋ ค๊ณ ํ๋๋ฐ, reload() ํจ์๊ฐ ์์ด์ง๊ฒ ์๋๊ฒ ๋๊ฐ!
๊ทธ๋์ ๊ณต์๋ฌธ์๋ฅผ ์ฐพ์๋ค์ด๊ฐ๋ค. ๊ณต์๋ฌธ์ ์๋ณธ์ ์ข ๊ฐ์ ธ์๋ดค๋ค.
useRouter()
- router.push(href: string, { scroll: boolean }):
Perform a client-side navigation to the provided route. Adds a new entry into the browser’s history stack.
"use client" ๋ฅผ ์ฌ์ฉํ client-side๋ก route๋ฅผ ์ ๊ณตํ ๋ ์ฌ์ฉ - router.replace(href: string, { scroll: boolean }): Perform a client-side navigation to the provided route without adding a new entry into the browser’s history stack.
์ญ์๋ client components ์์ ๋ผ์ฐํธ ์ธ ๋ ์ฌ์ฉ - - router.refresh(): Refresh the current route. Making a new request to the server, re-fetching data requests, and re-rendering Server Components. The client will merge the updated React Server Component payload without losing unaffected client-side React (e.g. useState) or browser state (e.g. scroll position).
reload()๋ฅผ ์ฐ์ง ์๊ณ ํ์ด์ง ๋ฆฌ๋ก๋๊ฐ ํ์ํ ์์ ์ฌ์ฉํ ์ ์๋ค. ์๋ฒ ์ปดํฌ๋ํธ ๋ฆฌ๋ ๋๋ง์ด ๋๋ค. - router.prefetch(href: string): Prefetch the provided route for faster client-side transitions.
href์ ๋ํ ํ์ด์ง prefetch ํ์ฌ ๋์ฑ ๋น ๋ฅธ ํด๋ผ์ด์ธํธ ์ฌ์ด๋ ํ์ด์ง ์ ํ์ ๊ตฌํํ ์ ์๋ค - router.back(): Navigate back to the previous route in the browser’s history stack.
- router.forward(): Navigate forwards to the next page in the browser’s history stack.
Migrating from next/router
- The useRouter hook should be imported from next/navigation and not next/router when using the App Router
import ํ ๋ 'next/router' ๊ฐ ์๋๋ผ 'next/navigation' ์ ์ฌ์ฉํด์ผํ๋ค. - The pathname string has been removed and is replaced by usePathname()
pathname ๋ฌธ์๋ ์์ด์ง๊ณ usePathname์ผ๋ก ๋ณ๊ฒฝ๋จ! - The query object has been removed and is replaced by useSearchParams()
query object ์ญ์ ์์ด์ง๊ณ useSearchParams๋ก ๋ณ๊ฒฝ๋จ - router.events has been replaced.
์์ ์๋ฌธ ์๋ณธ์ ๋ณด์ค ์ ์๋ ๊ณต์๋ฌธ์ ์ฃผ์!
- ๊ณต์๋ฌธ์
https://nextjs.org/docs/app/api-reference/functions/use-router
728x90
'๊ฐ๋ฐ > Next.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Nextjs] Route segment 'force-dynamic' ๊ฐ ํ์ํ ๋ (0) | 2024.03.29 |
---|---|
vercel๋ก Nextjs ๋ฐฐํฌํ๊ธฐ (0) | 2024.01.05 |
NEXT.js14 API Routes ์ฌ์ฉํ๊ธฐ (with. MONGODB) (1) | 2024.01.03 |
npm start, npm run dev ์ ์ฐจ์ด์ (1) | 2023.12.29 |
[Next.js (SSG) /cloudFront] ๋ฐฐํฌ ์ดํ reload์์ dynamic route๋ฅผ ๋ชป ํ ๋ ํด๊ฒฐ (0) | 2023.08.07 |