Next.js14์—์„œ useRouter ์‚ฌ์šฉํ•˜๊ธฐ

2024. 1. 3. 15:00ยท ๊ฐœ๋ฐœ/Next.js
๋ชฉ์ฐจ
  1. useRouter()
  2. Migrating from next/router
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

 

Functions: useRouter | Next.js

API reference for the useRouter hook.

nextjs.org

 

 

 

 

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
  1. useRouter()
  2. Migrating from next/router
'๊ฐœ๋ฐœ/Next.js' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [Nextjs] Route segment 'force-dynamic' ๊ฐ€ ํ•„์š”ํ•  ๋•Œ
  • vercel๋กœ Nextjs ๋ฐฐํฌํ•˜๊ธฐ
  • NEXT.js14 API Routes ์‚ฌ์šฉํ•˜๊ธฐ (with. MONGODB)
  • npm start, npm run dev ์˜ ์ฐจ์ด์ 
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป Frontend Developer
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
Ming devlog
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
์ „์ฒด
์˜ค๋Š˜
์–ด์ œ

๊ณต์ง€์‚ฌํ•ญ

  • About Ming ๐Ÿฐ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (261)
    • ๊ฐœ๋ฐœ (255)
      • ๐Ÿ“ TIL (58)
      • โŒ ์˜ค๋ฅ˜ ๋…ธํŠธ (23)
      • React (23)
      • Next.js (11)
      • React-Native (6)
      • JavaScript (10)
      • Flutter (19)
      • Database (1)
      • ๐Ÿ’ฌ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ (21)
      • ๐Ÿ“š English (9)
      • Etc... (25)
      • โ›ต ํ•ญํ•ด99 (49)
    • ๐Ÿ’ป Dev (6)
      • ๐Ÿ“ TIL (4)
      • ๐Ÿ’ฌ Algorithm (1)
      • Etc... (1)

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • flutter
  • react
  • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
  • ์ฝ”๋”ฉํ…Œ์ŠคํŠธ
  • ๋ฆฌ์•กํŠธ
  • twid
  • Firebase
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • ์ฝ”๋”ฉ๊ณต๋ถ€
  • ๊ฐœ๋ฐœ์žํšŒ๊ณ 
  • ํ”Œ๋Ÿฌํ„ฐ
  • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
  • ์ŠคํŒŒ๋ฅดํƒ€์ฝ”๋”ฉํด๋Ÿฝ
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ
  • leetcode
  • ๋ฆฌ์•กํŠธ๋„ค์ดํ‹ฐ๋ธŒ
  • react-native
  • ์ฝ”ํ…Œ๊ณต๋ถ€
  • ๋ถ€ํŠธ์บ ํ”„
  • nextjs
  • ์ฝ”ํ…Œ
  • ์˜์–ด๊ณต๋ถ€
  • ๊ฐœ๋ฐœ์ž์ผ๊ธฐ
  • TIL
  • ํ•ญํ•ด99
  • Git
  • ์ด์ฝ”๋…ธ๋ฏธ์ŠคํŠธ
  • github
  • todaywhatidid
  • ReactNative

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

250x250
hELLO ยท Designed By ์ •์ƒ์šฐ.v4.2.1
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
Next.js14์—์„œ useRouter ์‚ฌ์šฉํ•˜๊ธฐ
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”

๊ฐœ์ธ์ •๋ณด

  • ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ
  • ํฌ๋Ÿผ
  • ๋กœ๊ทธ์ธ

๋‹จ์ถ•ํ‚ค

๋‚ด ๋ธ”๋กœ๊ทธ

๋‚ด ๋ธ”๋กœ๊ทธ - ๊ด€๋ฆฌ์ž ํ™ˆ ์ „ํ™˜
Q
Q
์ƒˆ ๊ธ€ ์“ฐ๊ธฐ
W
W

๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๊ธ€

๊ธ€ ์ˆ˜์ • (๊ถŒํ•œ ์žˆ๋Š” ๊ฒฝ์šฐ)
E
E
๋Œ“๊ธ€ ์˜์—ญ์œผ๋กœ ์ด๋™
C
C

๋ชจ๋“  ์˜์—ญ

์ด ํŽ˜์ด์ง€์˜ URL ๋ณต์‚ฌ
S
S
๋งจ ์œ„๋กœ ์ด๋™
T
T
ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ ์ด๋™
H
H
๋‹จ์ถ•ํ‚ค ์•ˆ๋‚ด
Shift + /
โ‡ง + /

* ๋‹จ์ถ•ํ‚ค๋Š” ํ•œ๊ธ€/์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ ์ด์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ‹ฐ์Šคํ† ๋ฆฌ ๊ธฐ๋ณธ ๋„๋ฉ”์ธ์—์„œ๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.