[slug] ๊ฐ์ ์ด๋ป๊ฒ ํ์ฉํ ๊ฒ ์ธ๊ฐ? - pages/category/[slug].js
- slug ๊ฐ์ ๋ฐ๋ผ์ ์ฐ๋ฆฌ๊ฐ ์ํ๋ ํ์ด์ง๊ฐ ๋ค๋ฅด๊ฒ ๋ณด์ฌ์ ธ์ผ ํฉ๋๋ค.
1. useRouter
- Next.js ์์ ์ ๊ณตํ๊ณ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
import { useRouter } from 'next/router' // ์ ์ธ
const router = useRouter();
const { slug } = router.query;
1.1 category.js
import { useRouter } from "next/router";
const CategorySlug = () => {
const router = useRouter()
const { slug } = router.query
return (
<h1>Category {slug}</h1>
)
}
export default CategorySlug;
query ๊ฐ ์๋ค๋ฉด ?
/category/food?from=event
{"slug" : "food", "from" : "event" }
2. ๋ค์ค ์ฌ๋ฌ๊ทธ
2.1
- pages/[username]/[info].js
- const { username, info } = router.query
2.1.2 ํด๋ ๊ตฌ์กฐ
2.1.3 info.js
import { useRouter } from "next/router";
const userInfo = () => {
const router = useRouter()
const { username, info } = router.query
return (
<h1>Username's {username} {info}</h1>
)
}
export default userInfo;
3. slug ๋ ๋ฐฐ์ด
- pages/cart/[...slug].js
- const { slug } = router.query
3.1.1 ํด๋ ๊ตฌ์กฐ
4. ์ต์ ๋ slug
- ์ ๋ฒ ํฌ์คํ
์ฒ๋ผ
pages/cart/[...slug].js
๋ฅผ /cart ๋ก ์ ๊ทผํ๋ฉด 404๊ฐ ๋์ต๋๋ค. - ์ด์ ๋ ํด๋น ํด๋์ index.js ๊ฐ ์๊ธฐ ๋๋ฌธ์ ๋๋ค.
- ํ์ง๋ง
pages/cart/[[...slug]].js
ํด์ฃผ๋ฉด ์กด์ฌํ์ง ์์๋ ๊ธฐ๋ณธ ํ์ด์ง๊ฐ ์์ฑ ๋ฉ๋๋ค.
4.1 ํด๋ ๊ตฌ์กฐ
5. router.push
router ๋ฅผ ์ฌ์ฉํ๋ฉด์ ํ์ด์ง ์ด๋์ด ๊ฐ๋ฅ ํฉ๋๋ค.
5.1 ์ฝ๋
import Link from "next/link";
import { useRouter } from "next/router";
const CartSlug = () => {
const router = useRouter()
const { data } = router.query
return (
<>
<h1>CartSlug {JSON.stringify(data)}</h1>
<Link href="/cart/2022/08/07">
<a>2022๋
8์ 7์ผ๋ก</a>
</Link>
<button onClick={() => router.push('/cart/2022/08/10')}>2022๋
8์ 10์ผ๋ก</button>
</>
)
}
export default CartSlug;
6. Shallow Routing
- Next.js ์์ ํ์ด์ง๋ฅผ ๊ทธ๋ฆด ๋๋ pre-render ๋ฅผ ์ฐ์ ์ ์ผ๋ก ๊ณ ๋ ค ํฉ๋๋ค.
- getServerSideProps / getStaticProps ๋ฑ์ ๋ค์ ์คํ์ํค์ง ์๊ณ , ํ์ฌ ์ํ๋ฅผ ์์ง ์๊ณ url ์ ๋ฐ๊พธ๋ ๋ฐฉ๋ฒ
์๋ฅผ ๋ค์ด, ์ํ๋ ์ ์งํ๋ฉด์ url ๋ง ๋ฐ๊พธ๊ณ ์ถ์ ๊ฒฝ์ฐ ?
- ์ฌ์ฉ์๊ฐ ์ด๋ค ๋์์ ํ๊ณ , ๊ทธ ๊ธฐ๋ก์ query ๋ก ๋จ๊ธฐ๊ณ ์ถ์ ๋
- query ๋ก ๋จ๊ธฐ๋ฉด ์ฌ์ฉ์๊ฐ ์๋ก๊ณ ์นจ์ ํด๋ ์ ์ง ๋ฉ๋๋ค.
6.1 url ์ ๋ฐ๊พธ๋ 3๊ฐ์ง ๋ฐฉ์
- location.replace('url') : ๋ก์ปฌ state ์ ์ง ์๋จ ( ๋ฆฌ๋ ๋ )
- router.push(url) : ๋ก์ปฌ state ์ ์ง / data fetching ์ ์ผ์ด๋จ
- router.push(url, as, { shallow : true }) : ๋ก์ปฌ state ์ ์ง / data fetching X
6.1.1 location.replace
import { useRouter } from "next/router";
import { useState } from "react";
const userInfo = () => {
const router = useRouter()
const [clicked, setClicked] = useState(false)
const { status = 'initial'} = router.query
return (
<>
<h1>My Info</h1>
<h1>click : {String(clicked)}</h1>
<h1>Status : {status} </h1>
<button onClick={() => {
alert('change')
setClicked(true)
location.replace('/username/info?status=change')
}}>Edit : (replace)</button>
</>
)
}
export default userInfo;
- ๋ฒํผ์ ๋๋ฅด๋ฉด
true -> false
๊ฐ ๋๋ ์ด์ ๋ - url ์ ๋ฐ๊พธ๊ธฐ ์ํด์ ํ์ด์ง๋ฅผ ์๋ก ๊ฐ์ ธ์๊ธฐ ๋๋ฌธ ์ ๋๋ค.
6.1.2 router.push
import { useRouter } from "next/router";
import { useState } from "react";
const userInfo = () => {
const router = useRouter()
const [clicked, setClicked] = useState(false)
const { status = 'initial'} = router.query
return (
<>
<h1>My Info</h1>
<h1>click : {String(clicked)}</h1>
<h1>Status : {status} </h1>
<button onClick={() => {
alert('change')
setClicked(true)
router.push('/username/info?status=change')
}}>Edit : (push)</button>
</>
)
}
export async function getServerSideProps(){
console.log('server')
return {
props : {},
}
}
export default userInfo;
- ๋ก์ปฌ์ ์ํ๋ ์ ์ง๋์ง๋ง, ์๋ฒ์์ ์ง์์ ์ธ data fetching ์ ๋ฐ์ ํฉ๋๋ค.
6.1.3 shallow
import { useRouter } from "next/router";
import { useState } from "react";
const userInfo = () => {
const router = useRouter()
const [clicked, setClicked] = useState(false)
const { status = 'initial'} = router.query
return (
<>
<h1>My Info</h1>
<h1>click : {String(clicked)}</h1>
<h1>Status : {status} </h1>
<button onClick={() => {
alert('change')
setClicked(true)
router.push('/username/info?status=change', undefined, {shallow : true})
}}>Edit : (push)</button>
</>
)
}
export async function getServerSideProps(){
console.log('server')
return {
props : {},
}
}
export default userInfo;
- ๋ก์ปฌ์ ๊ฐ, URL ์ ๋ถ ๋ฐ๋๊ณ server ๋ํ ๋ถ๋ฆฌ์ง ์์ต๋๋ค.
- ์ ๋ง ์๋ฌด๊ฒ๋ ์๋ฐ๋๊ณ url ๋ง ๋ฐ๋ ์ํฉ์ ๋๋ค.
- ๋ฌผ๋ก ๋์ผํ ํ์ด์ง์์ query ๋ง ๋ฐ๋๋ ์ํฉ๋ง ์ ๋๋ค.
์ ๋ฆฌ
- Shallow Routing ์ Dynamic Routes -> slug ๋ฅผ ํ์ฉํ๋ ๋ฐฉ๋ฒ
- ๋ค์ค slug -> [user]/[info].js/ , [...slug].js
- ์ต์ ๋ slug -> [[...slug]].js
- Shallow Routing -> router.push(url, as, {shallow : true })
์ฐธ๊ณ
'๐ Front-End > Next.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Next.js - ๋ค๋ก๊ฐ๊ธฐ ํ ๋ ์ด์ ์คํฌ๋กค ์์น ์ ์ง (0) | 2022.12.21 |
---|---|
Next.js - Assets, Meta data, CSS (0) | 2022.10.26 |
Next.js - Link (0) | 2022.10.26 |
Next.js - Routing (0) | 2022.10.26 |
Next.js - Pre Rendering (0) | 2022.10.26 |