์ฌ์ฉํ๋ฉด์ ๋ฐฐ์ฐ๋ React Router
์์ํ๋ฉฐ
ํ ์ดํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉฐ React Router ์ ๋ํด ์๊ฒ๋ ์ ์ ๊ธฐ๋กํด๋์ !
์ ๋ฆฌ
exact
exact ํค์๋๋ ์ ํํ ์ผ์น ๋์์ ๋๋ง ์ง์ ๋ ์ปดํฌ๋ํธ๋ฅผ ๋ณด์ฌ์ฃผ๋ ์์ฑ์ด๋ค. ๋ง์ฝ exact ํค์๋ ์์ด ๋ผ์ฐํฐ๋ฅผ ์ง์ ํ๋ค๋ฉด {URL}/, {URL}/1 ๋ชจ๋ ๊ฐ์ ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋งํ๋ค.
<Route exact path={PATH} component={COMPONENT} /><Swtich>์์ ์ฐจ์ด
exact ํค์๋๋ง์ผ๋ก๋ ์๋ฌ๊ฐ ๋ฐ์ํ์ ๋์ ์ฒ๋ฆฌ๊ฐ ์ด๋ ต๋ค. ์๋์ ๊ฐ์ ๋ผ์ฐํฐ๊ฐ ์์ ๋ ์ง์ ๋์ง ์์ url์ ์ ๊ทผํ ๊ฒฝ์ฐ Error404 ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋ง ํ์ง ์๋๋ค๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
<Router>
<Route exact path="/" component={Foo} />
<Route path="/bar" component={Bar} />
<Route component={Error404} />
</Router>์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํ ๋ฐฉ๋ฒ์ผ๋ก ๋์จ ๊ฒ์ด <Switch>์ด๋ค. <Swtich>๋ ์ฒซ๋ฒ์งธ๋ก ๋งค์นญ๋๋ path๋ฅผ ๊ฐ์ง ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋ง ์ํจ๋ค. ์ด๊ฒ์ด exact์ ๋ค๋ฅธ์ ์ด๋ฉฐ, ์ง์ ๋์ง ์์ url์ ์ ๊ทผ ์ Error404 ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋ง ์ํจ๋ค๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
strict
strict ํค์๋๋ trailing slash๊ฐ ์์ ๊ฒฝ์ฐ์๋ง ์ผ์นํ๋ค.. ์ถ๊ฐ๋ก ์ฌ๋์ ๋ค์ URL์ด ์๋ ๊ฒฝ์ฐ์ ์ํฅ์ ๋ฏธ์น์ง ์๋๋ค.
| path | location.path | match |
|---|---|---|
/one |
/one |
no |
/one |
/one/ |
yes |
/one |
/one/two |
yes |
exact ํค์๋์ ํจ๊ป ์ฌ์ฉํ ๊ฒฝ์ฐ ์ด๋ ๊ฒ ์๋ํ๋ค.
| path | location.path | match |
|---|---|---|
/one |
/one |
yes |
/one |
/one/ |
no |
/one |
/one/two |
no |
sensitive
sensitive ํค์๋๋ case๋ฅผ ๊ฒ์ฌํ๋ค.
| path | location.path | sensitive | match |
|---|---|---|---|
/one |
/one |
true |
yes |
/One |
/one |
true |
no |
/One |
/one |
false |
yes |