MapleStory Finger Point

๐Ÿ”ต React

[React] React๋ผ์šฐํ„ฐ

HYEJU01 2024. 8. 28. 23:33

๐Ÿ˜บ React ๋ผ์šฐํ„ฐ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ธฐ ์ „์— SPA ์˜ ๊ฐœ๋…์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.

 

๐Ÿ“ SPA๋ž€? (single page application)

ํ•˜๋‚˜์˜ ํŽ˜์ด์ง€๋กœ ๋งŒ๋“ค์–ด์ง„ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์˜๋ฏธํ•œ๋‹ค.

์ „ํ†ต์ ์ธ ๋ฐฉ์‹์€ Server๊ฐ€ html ํŒŒ์ผ ๋งŒ๋“ค๊ณ  data ์‹ค์–ด์„œ ์‘๋‹ตํ•˜์ง€๋งŒ,

spa ๋ฐฉ์‹์€ ํด๋ผ์ด์–ธํŠธ์— html ์„ธ์›Œ๋‘๊ณ  ๋ฐ์ดํ„ฐ๋งŒ ์š”์ฒญ์‘๋‹ต์„ ํ†ตํ•ด ๋ฐ”๊พผ๋‹ค.

 

 

  1. SPA๋Š” htmlํŒŒ์ผ์„ ๋ธŒ๋ผ์šฐ์ € ์ธก์—์„œ ๋กœ๋“œํ•˜๊ณ , ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋Š” API์™€ ajaxํ†ต์‹ ์„ ์ด์šฉํ•ด์„œ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  2. ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ƒํ˜ธ์ž‘์šฉ ํ•˜๋ฉด ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ์—…๋ฐ์ดํŠธ ํ•ด์„œ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  3. ๋ฉ€ํ‹ฐํ”Œ๋žซํผ Android, IOS์— ๋Œ€์‘ํ•˜์—ฌ ์›น๋ทฐ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ชฉ์ ์œผ๋กœ๋„ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

 

๐Ÿ“ SPA์˜ ๋‹จ์  (CSR๋ฐฉ์‹)

  • ์•ฑ์˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์ง€๋ฉด, JSํŒŒ์ผ๋„ ๋„ˆ๋ฌด ์ปค์ ธ์„œ ๋กœ๋”ฉ์ด ์˜ค๋ž˜๊ฑธ๋ฆฌ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  • ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋ Œ๋”๋ง์ด ์™„๋ฃŒ๋˜๊ธฐ ๊นŒ์ง€ ๋น„์–ด์žˆ๋Š” ํ™”๋ฉด์ด ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  • ๊ทธ๋ž˜์„œ ๊ทœ๋ชจ๊ฐ€ ํฐ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์€ SSR(์„œ๋ฒ„์‚ฌ์ด๋“œ ๋ Œ๋”๋ง) ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. (์›นํŒฉ ์„ค์ • ํ•„์š”)

 


 

 

๐Ÿ˜บ ์ด์ œ ๋ผ์šฐํŒ…์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.  !

 

 

๐Ÿ“  ๋ผ์šฐํŒ…์ด๋ž€?

  • ๋ธŒ๋ผ์šฐ์ €์˜ ์ฃผ์†Œ์ƒํƒœ์— ๋”ฐ๋ผ ๋‹ค์–‘ํ•œ ํ™”๋ฉด์„ ๋ณด์—ฌ์ฃผ๋„๋ก ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์„ ๋ผ์šฐํŒ…์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ“  ์‚ฌ์šฉ์ด์œ 

  • create-react-app๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ฒŒ ๋˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ SPA์— CSR(ํด๋ผ์ด์–ธํŠธ ๋ Œ๋”๋ง) ์ž…๋‹ˆ๋‹ค.
  • ํ•˜๋‚˜์˜ ํŽ˜์ด์ง€๋งŒ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • SPA์ด์ง€๋งŒ ๋ผ์šฐํ„ฐ๋ฅผ ํ™œ์šฉํ•ด์„œ ์‚ฌ์šฉ์ž๋กœ ํ•˜์—ฌ๊ธˆ ์—ฌ๋Ÿฌ ํŽ˜์ด์ง€๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋Š๊ปด์ง€๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


 

๐Ÿ˜บ ๋ผ์šฐํ„ฐ๋Š” ์„ค์น˜ํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค!

 

๐Ÿ“ ๋ฆฌ์•กํŠธ ๋ผ์šฐํ„ฐ ์„ค์น˜ ๋ฐฉ๋ฒ•

 

 

1.๋ผ์šฐํ„ฐ๋ฅผ ์ ์šฉํ•  ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

npm create react-app ํ”„๋กœ์ ํŠธ๋ช…

 

2.๋ผ์šฐํ„ฐ ์„ค์น˜

npm install react-router-dom

 

3. ํ”„๋กœ์ ํŠธ ์‹œ์ž‘

npm start

 

 

 

 

๐Ÿ“  ๋ผ์šฐํ„ฐ ์ ์šฉํ•˜๊ธฐ

  1. ๋ผ์šฐํ„ฐ๋ฅผ ์‚ฌ์šฉํ• ๋•Œ๋Š” root app ์ตœ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ index.js์—์„œ App์ปดํฌ๋„ŒํŠธ๋ฅผ <BrowserRouter>๋กœ ๊ฐ์‹ธ์ค๋‹ˆ๋‹ค.
  2. ํŽ˜์ด์ง€ ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ
  3. App์ปดํฌ๋„ŒํŠธ์—์„œ <Routes>๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ถ„๊ธฐํ•˜๊ธฐ
  4. ์ฃผ์†Œ์š”์ฒญ์„ ํ†ตํ•ด ํ™•์ธํ•˜๊ธฐ

 


 

๐Ÿ˜บ ๋ผ์šฐํ„ฐ ์ข…๋ฅ˜๋Š” ์–ด๋–ค ๊ฒƒ์ด ์žˆ์„๊นŒ?

 

 

  • <Routes>: ๋ชจ๋“  Route ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐ์‹ธ๋Š” ์ปจํ…Œ์ด๋„ˆ ์ปดํฌ๋„ŒํŠธ์ž…๋‹ˆ๋‹ค. Route ์ปดํฌ๋„ŒํŠธ๋Š” ์ž์‹์œผ๋กœ ๋ฐ›์•„์„œ ๋ผ์šฐํŒ…์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  • <Route>: ๊ฒฝ๋กœ์™€ ํ•ด๋‹น ๊ฒฝ๋กœ์— ๋ Œ๋”๋งํ•  ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ค‘์ฒฉ ๋ผ์šฐํŠธ๋ฅผ ์ง€์›ํ•˜๋ฉฐ, element ์†์„ฑ์œผ๋กœ ๋ Œ๋”๋งํ•  JSX ์š”์†Œ๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
  • <Navigate>: ํŽ˜์ด์ง€๋ฅผ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์ž…๋‹ˆ๋‹ค. to ์†์„ฑ์œผ๋กœ ์ด๋™ํ•  ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜๊ณ , replace ์†์„ฑ์œผ๋กœ ํ˜„์žฌ ํŽ˜์ด์ง€๋ฅผ ๊ต์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • <Link>: ํŽ˜์ด์ง€ ๊ฐ„ ๋„ค๋น„๊ฒŒ์ด์…˜์„ ์œ„ํ•œ ๋งํฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. to ์†์„ฑ์œผ๋กœ ์ด๋™ํ•  ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • <NavLink>: ํ™œ์„ฑ ๋งํฌ ์Šคํƒ€์ผ๋ง์„ ์ง€์›ํ•˜๋Š” ๋งํฌ ์ปดํฌ๋„ŒํŠธ์ž…๋‹ˆ๋‹ค. to ์†์„ฑ์œผ๋กœ ์ด๋™ํ•  ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜๊ณ , isActive ์†์„ฑ์œผ๋กœ ํ™œ์„ฑ ์ƒํƒœ์— ๋Œ€ํ•œ ์Šคํƒ€์ผ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • <Outlet>: ์ค‘์ฒฉ ๋ผ์šฐํŠธ์˜ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง๋˜๋Š” ์ž๋ฆฌ์ž…๋‹ˆ๋‹ค. ๋ถ€๋ชจ ๋ผ์šฐํŠธ์˜ ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • useNavigate: ํ”„๋กœ๊ทธ๋žจ์ ์œผ๋กœ ๋„ค๋น„๊ฒŒ์ด์…˜์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ›…์ž…๋‹ˆ๋‹ค. navigate ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํŽ˜์ด์ง€๋ฅผ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
  • useParams: URL ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ํ›…์ž…๋‹ˆ๋‹ค. URL์—์„œ ๋™์ ์œผ๋กœ ๊ฐ’์„ ์ฝ์–ด์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • useLocation: ํ˜„์žฌ ์œ„์น˜ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ํ›…์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ URL, ๊ฒ€์ƒ‰ ํŒŒ๋ผ๋ฏธํ„ฐ, ์ƒํƒœ ๋“ฑ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • useSearchParams: ์ฟผ๋ฆฌ ๋ฌธ์ž์—ด์˜ ๊ฒ€์ƒ‰ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์„ค์ •ํ•˜๋Š” ํ›…์ž…๋‹ˆ๋‹ค.

 

 

 


 

<Link> ๋ฅผ ์ด์šฉํ•ด์„œ ๋‹ค๋ฅธ ํŽ˜์ด์ง€๋กœ ์ด๋™

 

 

[App.js]

import {Route, Routes} from 'react-router-dom';
import Home from './component/Home';
import Info from './component/Info';
import User from './component/User';
import Board from './component/Board';
import BoardContent from './component/BoardContent';
import Header from './layout/Header';
import MyPage from './component/MyPage';

function App() {
  return (
    // ์ค‘์ฒฉ๋ผ์šฐํ„ฐ
    <Routes>

      <Route  element={<Header/>}> 
      <Route path='/' element={<Home/>} />
      <Route path='/info/:num' element={<Info/>} />
      <Route path='/user' element={<User/>} />
      </Route>
      
      {/* <Route path='/board' element={<Board/>} />
      <Route path='/board/:num' element={<BoardContent/>} /> */}

      {/* ์ค‘์ฒฉ ๋ผ์šฐํ„ฐ */}
      <Route path='/board' element={<Board/>}>
        <Route path=':num' element={<BoardContent/>} />
      </Route>

      {/* navigation ์ปดํฌ๋„ŒํŠธ */}
      <Route path='/mypage' element={<MyPage/>} />
        


    </Routes>
  );
}

export default App;

 

 

[home.js]

import { Link } from "react-router-dom";


function Home() {

  return (
    <div>
      <h3>ํ™ˆํŽ˜์ด์ง€ ์ž…๋‹ˆ๋‹ค</h3>  
      <Link to='/user?name=ํ™๊ธธ๋™&age=20'>userํŽ˜์ด์ง€</Link>
      <br/>
      <Link to='/info/1'>infoํŽ˜์ด์ง€</Link>
      <Link to='/info/2'>infoํŽ˜์ด์ง€</Link>
      <Link to='/info/3'>infoํŽ˜์ด์ง€</Link>


    </div>
  )
}

export default Home;

 

  • <Link> ์ปดํฌ๋„ŒํŠธ: React Router์—์„œ ์ œ๊ณตํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋กœ, SPA(Single Page Application)์—์„œ ํŽ˜์ด์ง€ ์ƒˆ๋กœ๊ณ ์นจ ์—†์ด ๋‹ค๋ฅธ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.
  • to='/info/1': to ์†์„ฑ์€ ์ด๋™ํ•  ๋Œ€์ƒ ๊ฒฝ๋กœ(URL)๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ๋งํฌ๋ฅผ ํด๋ฆญํ•˜๋ฉด /info/1 ๊ฒฝ๋กœ๋กœ ์ด๋™ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

 

 

 


 

URL ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง

 

 

๐Ÿ˜บ ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๋Š” ๋ฐฉ์‹์€ urlํŒŒ๋ผ๋ฏธํ„ฐ , ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์ด ์žˆ๋‹ค.

 

๊ฐ™์€ ์ปดํฌ๋„ŒํŠธ๋ผ๋„ ์ „๋‹ฌ๋˜๋Š” ๋ฐ์ดํ„ฐ์— ๋”ฐ๋ผ์„œ ๋‹ค๋ฅธ ๋‚ด์šฉ์„ ๋ณด์—ฌ์ค˜์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

EX) ๊ธ€ ์ƒ์„ธ

์ด ๋•Œ URL์ฃผ์†Œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌ ํ•  ์ˆ˜ ์žˆ๊ณ , ์ปดํฌ๋„ŒํŠธ๋Š” ๊ทธ ๊ฐ’์„ ๋ฐ›์•„์„œ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

๐Ÿ˜บ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง ( /๊ฒฝ๋กœ?ํ‚ค=๊ฐ’&ํ‚ค=๊ฐ’)

  • ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์€ ์ฃผ์†Œ์˜ ? ๋’ค์— ํ‚ค=๊ฐ’ ์˜ ํ˜•ํƒœ๋กœ ๋„˜์–ด๊ฐ€๋Š” ๋งค๊ฐœ๊ฐ’์ž…๋‹ˆ๋‹ค.
  • ๋ณ„๋„์˜ ๋ผ์šฐํ„ฐ ์„ค์ •์€ ํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค.
  • ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” useLocation() ํ›… or useSearchParams() ํ›…์„ ์‚ฌ์šฉ ํ•ด์„œ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

๐Ÿ˜บ ๋งํฌ์—์„œ

 

๐Ÿ“  /๊ฒฝ๋กœ?ํ‚ค=๊ฐ’&ํ‚ค=๊ฐ’

      <Link to='/user?name=ํ™๊ธธ๋™&age=20'>userํŽ˜์ด์ง€</Link>

 

 

๐Ÿ˜บ์ปดํฌ๋„ŒํŠธ์—์„œ

 

๐Ÿ“ useLocation() ํ›…

const location = useLocation();

 

  • useLocation() ํ›… : React Router์—์„œ ํ˜„์žฌ URL์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ํ›…
  • pathname: ํ˜„์žฌ ๊ฒฝ๋กœ (์˜ˆ: /info/1)
  • search: ์ฟผ๋ฆฌ ๋ฌธ์ž์—ด (์˜ˆ: ?name=John&age=30)
  • hash: URL์˜ ํ•ด์‹œ ๋ถ€๋ถ„ (์˜ˆ: #section1)
  • state: Link ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฒฝ๋กœ๋ฅผ ๋ณ€๊ฒฝํ•  ๋•Œ ์ „๋‹ฌํ•œ ์ƒํƒœ ๊ฐ์ฒด

 

 

 

๐Ÿ“  useSearchParams() ํ›…

const [๊ฐ์ฒด, function] = useSearchParams()
  • ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋Š” ์ฟผ๋ฆฌํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•˜๋Š” get, set์ด ๋‹ด๊ธด ๊ฐ์ฒด
  • ๋‘ ๋ฒˆ์งธ ์š”์†Œ๋Š” ์ฟผ๋ฆฌํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ์ฒด๋กœ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ํ•จ์ˆ˜ ๋ฐ˜ํ™˜

 

  • useSearchParams() ํ›… : React Router์—์„œ URL์˜ ์ฟผ๋ฆฌ ๋ฌธ์ž์—ด(search parameters)์„ ์ฝ๊ณ  ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ํ›…(Hook)
  • [searchParams, setSearchParams]์˜ ํ˜•ํƒœ๋กœ ๊ฐ’์„ ๋ฐ˜ํ™˜ !
  • searchParams.get('query'): query๋ผ๋Š” ์ด๋ฆ„์˜ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค
    const [obj, setObj] = useSearchParams();
    let id = obj.get("id");//request.getParameter("ํ‚ค")
    let key = obj.get("key");

 

 

 

๐Ÿ˜บ URLํŒŒ๋ผ๋ฏธํ„ฐ (/๊ฒฝ๋กœ/๊ฐ’/๊ฐ’) ๐Ÿ‘

  • URLํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์ฃผ์†Œ ๋’ค์— /๊ฒฝ๋กœ/๊ฐ’/๊ฐ’ ์˜ ํ˜•ํƒœ๋กœ ๋„˜์–ด๊ฐ€๋Š” ๋งค๊ฐœ๊ฐ’์ž…๋‹ˆ๋‹ค.
  • ๋ผ์šฐํ„ฐ์— ์ถ”๊ฐ€์ ์ธ ์„ค์ •์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ( /์ฃผ์†Œ/:ํ‚ค )
  • ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” useParams() ํ›…์„ ์‚ฌ์šฉํ•˜์—ฌ URLํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ˜บ ๋ผ์šฐํ„ฐ์—์„œ

  <Routes>
        .....
        <Route path='/info/:num' element={<Info/>}/>
    </Routes>

 

 

๐Ÿ˜บ๋งํฌ์—์„œ

<li><Link to='/info/1'>1๋ฒˆ info</Link></li>

 

 

๐Ÿ˜บ์ปดํฌ๋„ŒํŠธ์—์„œ 

 

๐Ÿ“ useParams() ํ›…

/param์—๋Š” link๋กœ ๋„˜์–ด์˜จ ํ‚ค๊ฐ€ ๋‹ด๊ฒจ ์žˆ์Šต๋‹ˆ๋‹ค.

let param = useParams();

 

 


์ค‘์ฒฉ๋ผ์šฐํ„ฐ๋กœ ๊ณตํ†ต ๋ถ€๋ถ„ ์ฒ˜๋ฆฌํ•˜๊ธฐ

 

๐Ÿ˜บ ์ค‘์ฒฉ ๋ผ์šฐํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ณตํ†ต ๋ถ€๋ถ„์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ๋œ๋‹ค. 

  • ๊ธ€ํŽ˜์ด์ง€๊ฐ€ ์žˆ๊ณ , ๊ธ€์— ๋”ฐ๋ฅธ ์ƒ์„ธํ™”๋ฉด์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค.
  • ๋ผ์šฐํ„ฐ์˜ ์„ค์ •์€ ์•„๋ž˜ ์ฒ˜๋Ÿผ ๋ณด์—ฌ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์•„๋ž˜ ์„ค์ •์€ ๋‘ ๋ผ์šฐํ„ฐ๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ๊ฐ ๋‹ค๋ฅธ ํ™”๋ฉด์ด ๋ณด์ž…๋‹ˆ๋‹ค.

 

 


๐Ÿ˜บ  ์ค‘์ฒฉ ๋ผ์šฐํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ผ์šฐํŠธ ๋‚ด์— ๋‹ค๋ฅธ ๋ผ์šฐํŠธ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์–ด

๋ณต์žกํ•œ URL ๊ตฌ์กฐ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค !

 

  • <Routes>: ๋ชจ๋“  ๋ผ์šฐํŠธ๋ฅผ ๊ฐ์‹ธ๋Š” ์ปจํ…Œ์ด๋„ˆ ์ปดํฌ๋„ŒํŠธ์ž…๋‹ˆ๋‹ค. Route ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ž์‹์œผ๋กœ ๋ฐ›์•„ ๋ผ์šฐํŒ…์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
  • <Route>: ๊ฒฝ๋กœ์™€ ํ•ด๋‹น ๊ฒฝ๋กœ์— ๋ Œ๋”๋งํ•  ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ค‘์ฒฉ๋œ Route๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•˜์œ„ ๋ผ์šฐํŠธ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • element: ๋ Œ๋”๋งํ•  ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. JSX ์š”์†Œ๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
  • path: ๋ผ์šฐํŠธ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ค‘์ฒฉ ๊ฒฝ๋กœ๋Š” ๋ถ€๋ชจ Route์˜ ๊ฒฝ๋กœ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค.
  • <Outlet>: ์ค‘์ฒฉ๋œ ๋ผ์šฐํŠธ์˜ ์ฝ˜ํ…์ธ ๊ฐ€ ๋ Œ๋”๋ง๋˜๋Š” ์ž๋ฆฌ์ž…๋‹ˆ๋‹ค. ๋ถ€๋ชจ ๋ผ์šฐํŠธ์˜ ์ปดํฌ๋„ŒํŠธ ๋‚ด์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

๐Ÿ“ ์ค‘์ฒฉ๋ผ์šฐํ„ฐ

 

Routes์™€ Route๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณต์žกํ•œ ๋ผ์šฐํŠธ ๊ตฌ์กฐ๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ค‘์ฒฉ๋œ Route๋Š” ๋ถ€๋ชจ ๋ผ์šฐํŠธ์˜ ์ฝ˜ํ…์ธ  ๋‚ด์— ์ž์‹ ์ฝ˜ํ…์ธ ๋ฅผ ๋ Œ๋”๋ง

 

  • <Outlet>: ์ค‘์ฒฉ๋œ ๊ฒฝ๋กœ์˜ ์ฝ˜ํ…์ธ ๊ฐ€ ๋ Œ๋”๋ง๋  ์œ„์น˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  • ๋™์  ๊ฒฝ๋กœ: URL ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ ์œผ๋กœ ๊ฒฝ๋กœ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
<Routes>
	<Route path='/board' element={<Board/>}/>
	<Route path='/board/:num' element={<BoardContent/>}/>
</Routes>

 

 

  • ๋งŒ์•ฝ Board ๋ชฉ๋ก ํŽ˜์ด์ง€๋ฅผ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์ƒ์„ธํŽ˜์ด์ง€๋ฅผ ์„œ๋ธŒ๋กœ ๋ณด์—ฌ์ฃผ๋„๋ก ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด ์ค‘์ฒฉ๋ผ์šฐํ„ฐ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ค‘์ฒฉ๋ผ์šฐํ„ฐ๋กœ ์ ์šฉ๋˜๋ฉด ๋ถ€๋ชจ์ปดํฌ๋„ŒํŠธ์—์„œ <Outlet> ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ™œ์šฉํ•ด์„œ ํ•˜์œ„ ๋ผ์šฐํ„ฐ๋ฅผ ๋ณด์—ฌ์ง€๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
<Routes>
	<Route path='/board' element={<Board/>}>
		<Route path=':num' element={<BoardContent/>}/>
	</Route>

</Routes>

 

 

 

 

 


๋ผ์šฐํ„ฐ ๋ถ€๊ฐ€ ๊ธฐ๋Šฅ (Link, Navigate)


๐Ÿ˜บ   NavLink ์ปดํฌ๋„ŒํŠธ

  • ๋งํฌ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ๋กœ๊ฐ€ ๋ผ์šฐํ„ฐ์˜ ๊ฒฝ๋กœ์™€ ์ผ์น˜ํ•˜๋ฉด ํŠน์ •์Šคํƒ€์ผ์„ ์ ์šฉ (ํ™œ์„ฑํ™”๊ธฐ๋Šฅ)
  • NavLink๋Š” ๋งํฌ์˜ ๊ฒฝ๋กœ๊ฐ€ ๋ผ์šฐํ„ฐ์˜ ๊ฒฝ๋กœ์™€ ์ผ์น˜ํ•˜๋ฉด ํŠน์ •์Šคํƒ€์ผ์„ ์ ์šฉํ•ด ์ค๋‹ˆ๋‹ค.
  • NavLink๋Š” style์†์„ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค
  • style์†์„ฑ์—๋Š” ์‹คํ–‰์‹œํ‚ฌ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • ์ด ํ•จ์ˆ˜์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ {isActive : boolean}๊ฐ์ฒด๋ฅผ ๋„ฃ์–ด์ฃผ๋Š”๋ฐ, ํ™œ์„ฑํ™” ์—ฌ๋ถ€๋ฅผ ํ‘œ์‹œ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  • ์‚ฌ์šฉ์€ ๋ฐ˜๋“œ์‹œ {isActive}๋ณ€์ˆ˜๋กœ ๊ตฌ์กฐ๋ถ„ํ•ดํ• ๋‹น ํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ“ Link
Link๋Š” ๊ธฐ๋ณธ์ ์ธ ๋„ค๋น„๊ฒŒ์ด์…˜์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ

 


๐Ÿ“   NavLink
NavLink๋Š” Link์˜ ํ™•์žฅ์œผ๋กœ, ํ˜„์žฌ ํŽ˜์ด์ง€์™€์˜ ์ผ์น˜ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ๋™์ ์œผ๋กœ ์Šคํƒ€์ผ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณต

 

 

 

 

 

 

๐Ÿ˜บ useNavigate() ํ›… vs Navigate ์ปดํฌ๋„ŒํŠธ

 

 

 

๐Ÿ“ useNavigate() ํ›…

let navigator = useNavigate();
  • JS์˜ history๊ฐ์ฒด๋ฅผ ๋Œ€์‹ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋ฒคํŠธ์—์„œ ์‚ฌ์šฉ
  • useNavigateํ›… ์€ ํŠน์ • event๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ, url์„ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
  • react v6 ์—์„œ useHistory ๊ฐ€ ๋ณ€ํ™”ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. (JS์˜ history๊ฐ์ฒด๋ฅผ ๋Œ€์‹  ํ•ฉ๋‹ˆ๋‹ค)
  • JS์˜ history๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ธฐ๋Šฅ๋„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

 

  • ํ˜ธ์ถœ ์œ„์น˜: ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค
  • ์‚ฌ์šฉ ๋ฐฉ๋ฒ•: ๋„ค๋น„๊ฒŒ์ด์…˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ ์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
  • ์žฅ์ : ์‚ฌ์šฉ์ž ์ธํ„ฐ๋ž™์…˜(๋ฒ„ํŠผ ํด๋ฆญ ๋“ฑ)๊ณผ ๊ฐ™์€ ์ด๋ฒคํŠธ์—์„œ ๋„ค๋น„๊ฒŒ์ด์…˜์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค

 

 

๐Ÿ“   Navigate ์ปดํฌ๋„ŒํŠธ

 

  • ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๊ธฐ๋Šฅ
  • ๋ Œ๋”๋ง์‹œ์— ์‚ฌ์šฉ
  • <Navigate> ์ปดํฌ๋„ŒํŠธ๋Š” ๋ Œ๋”๋ง ๋  ๋•Œ ํ˜„์žฌ ์œ„์น˜๋ฅผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. (๋ Œ๋”๋ง ์ค‘ ๋งŒ๋‚˜๊ฒŒ ๋˜๋ฉด, ํŽ˜์ด์ง€๋ฅผ ๊ฐ•์ œ๋กœ ์ด๋™์‹œํ‚ด)
  • useNavigate()ํ›…๊ณผ ๋น„์Šทํ•ด๋ณด์ด์ง€๋งŒ useNavigate()ํ›…์€ ๋ Œ๋”๋ง ๊ณผ์ •์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

 

  • ํ˜ธ์ถœ ์œ„์น˜: JSX ๋‚ด์—์„œ ์ง์ ‘ ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค.
  • ์‚ฌ์šฉ ๋ฐฉ๋ฒ•: ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง๋  ๋•Œ ์ž๋™์œผ๋กœ ํŽ˜์ด์ง€๋ฅผ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•ฉ๋‹ˆ๋‹ค.
  • ์žฅ์ : ์กฐ๊ฑด๋ถ€ ๋ฆฌ๋‹ค์ด๋ ‰์…˜์ด๋‚˜, ๋ Œ๋”๋ง ์‹œ์ ์—์„œ ๋„ค๋น„๊ฒŒ์ด์…˜์„ ๊ตฌํ˜„ํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • to: ์ด๋™ํ•  ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ๋˜๋Š” ๊ฒฝ๋กœ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • replace: true๋กœ ์„ค์ •ํ•˜๋ฉด ํ˜„์žฌ ํŽ˜์ด์ง€๋ฅผ ํžˆ์Šคํ† ๋ฆฌ ์Šคํƒ์˜ ์ตœ์ƒ์œ„์—์„œ ๊ต์ฒดํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ธŒ๋ผ์šฐ์ €์˜ "๋’ค๋กœ ๊ฐ€๊ธฐ" ๋ฒ„ํŠผ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด์ „ ํŽ˜์ด์ง€๋กœ ๋Œ์•„๊ฐˆ ์ˆ˜ ์—†๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ false์ž…๋‹ˆ๋‹ค.
  • state: ์ด๋™ํ•  ํŽ˜์ด์ง€์— ์ƒํƒœ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ƒํƒœ๋Š” location.state๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

 

 

 


[user.js]

import { useSearchParams } from "react-router-dom";



function User() {

  //1. useLocationํ›…์„ ์‚ฌ์šฉ
  // const location = useLocation();
  // const {search, pathname} = location;
  // console.log(search);
  // console.log(pathname);
  
  //2. useSearchParamsํ›…์„ ์‚ฌ์šฉ
  const searchParam = useSearchParams();
  console.log(searchParam);

  const [obj, setObj] =  useSearchParams();
  console.log(obj);
  console.log(setObj);
  let name = obj.get("name");
  let age = obj.get("age");
  console.log(name, age);
  
  const handleClick = () => {
    setObj({name : '์ด์ˆœ์‹ ', age : 30}); // ๊ฐ‘ ๋ณ€๊ฒฝํ•˜๊ธฐ
  }

  return (
    <div>
      <h3>์œ ์ €ํŽ˜์ด์ง€ ์ž…๋‹ˆ๋‹ค</h3>
      <button onClick={handleClick}> uri ๊ฐ’ ๋ณ€๊ฒฝ </button>
    </div>
  )
}

export default User;
  • useLocation() ํ›… : React Router์—์„œ ํ˜„์žฌ URL์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ํ›…

 

 

//2. useSearchParamsํ›…์„ ์‚ฌ์šฉ
  const searchParam = useSearchParams();
  console.log(searchParam);
  
  const [obj, setObj] =  useSearchParams();
  console.log(obj);
  
  let name = obj.get("name");
  let age = obj.get("age");
  console.log(name, age);

 

  • useSearchParams() ํ›… : React Router์—์„œ URL์˜ ์ฟผ๋ฆฌ ๋ฌธ์ž์—ด(search parameters)์„ ์ฝ๊ณ  ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ํ›…(Hook)
  • [searchParams, setSearchParams]์˜ ํ˜•ํƒœ๋กœ ๊ฐ’์„ ๋ฐ˜ํ™˜ !
  • searchParams.get('query'): query๋ผ๋Š” ์ด๋ฆ„์˜ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค

 

 

  const handleClick = () => {
    setObj({name : '์ด์ˆœ์‹ ', age : 30}); // ๊ฐ‘ ๋ณ€๊ฒฝํ•˜๊ธฐ
  }
  • seobj ๋ฅผ ํ†ตํ•ด์„œ uri ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 


 


[info.js]

import { useParams } from "react-router-dom";


function Info() {

  //์˜ˆ์‹œ๋ฐ์ดํ„ฐ
  const data = {
    "1" : {id:1 , name: "ํ™๊ธธ๋™"},
    "2" : {id:2 , name: "์ด์ˆœ์‹ "},
    "3" : {id:3 , name: "์‹ ์‚ฌ์ž„๋‹น"}
  }


  //useParamsํ›… - URLํŒŒ๋ผ๋ฏธํ„ฐ 
  let param = useParams();
  console.log(param);
  console.log(param.num); //์ฐพ์•„์„œ ์‚ฌ์šฉํ•จ
  const {id, name} = data[param.num];

  return (
    <div>
      <h3>InfoํŽ˜์ด์ง€ ์ž…๋‹ˆ๋‹ค</h3>
      <div>
        ์„ ํƒ๋œ ๋ฐ์ดํ„ฐ๋Š” {id}์ด๊ณ  {name}์ž…๋‹ˆ๋‹ค.
      </div>

    </div>
  )
}

export default Info;

 

  const data = {
    "1" : {id:1 , name: "ํ™๊ธธ๋™"},
    "2" : {id:2 , name: "์ด์ˆœ์‹ "},
    "3" : {id:3 , name: "์‹ ์‚ฌ์ž„๋‹น"}
  }
  • ํ‚ค:๊ฐ’{ํ‚ค:๊ฐ’, ํ‚ค:๊ฐ’} ํ˜•ํƒœ๋กœ ๋“ค์–ด๊ฐ€์žˆ๋Š” data ๊ฐ์ฒด

 

 

  let param = useParams();
  console.log(param);
  console.log(param.num); //์ฐพ์•„์„œ ์‚ฌ์šฉํ•จ
  const {id, name} = data[param.num];

  • useParams() ํ›… : React Router์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํ›…์œผ๋กœ, URL ๊ฒฝ๋กœ์— ํฌํ•จ๋œ ๋งค๊ฐœ๋ณ€์ˆ˜(params)๋ฅผ ์ฝ๋Š” ๋ฐ ์‚ฌ์šฉ
  • param.num ์˜ ๊ฐ’๊ณผ ๋Œ€์‘ํ•˜๋Š” data ํ‚ค๋ฅผ ์ฐพ๊ณ  ๊ทธ ๊ฐ’์„ ํ•ด๋‹น ํ•ญ๋ชฉ์˜ id์™€ name ์†์„ฑ์„ ์ถ”์ถœ

 

 

[info/1]

[info/2]

[info/3]


[header.js]

import { Outlet, useNavigate } from 'react-router-dom';
import style from './Header.module.css';


function Header() {

  let nav = useNavigate();

  const goBack = () => {
    nav(-1);
  }

  const goHome = () => {
    nav('/');
  }

  return (

  <>

  <header className={style.wrap}>

      <h3> ํ—ค๋”์ž…๋‹ˆ๋‹ค. </h3>

      <ul className={style.wrap_list}> 

        <li> ๋ชฉ๋ก </li>
        <li> ๋ชฉ๋ก </li>
        <li> ๋ชฉ๋ก </li>
        <li> ๋ชฉ๋ก </li>

      </ul>
      <div>
        <button onClick={goHome}> ํ™ˆ์œผ๋กœ </button>
        <button onClick={goBack}> ๋’ค๋กœ๊ฐ€๊ธฐ </button>
      </div>


  </header>
  
  <section>
        <Outlet />
      </section>

  </>

  )

}

export default Header;

 

  • useNavigate () ํ›… : react-router-dom ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ œ๊ณตํ•˜๋ฉฐ, ๋„ค๋น„๊ฒŒ์ด์…˜(ํŽ˜์ด์ง€ ์ด๋™) ๊ธฐ๋Šฅ์„ ์ œ๊ณต
  • ์ด ํ›…์€ ๋„ค๋น„๊ฒŒ์ด์…˜ ํ•จ์ˆ˜ nav๋ฅผ ๋ฐ˜ํ™˜
  • nav(-1)์„ ํ˜ธ์ถœํ•˜์—ฌ ๋ธŒ๋ผ์šฐ์ €์˜ ํžˆ์Šคํ† ๋ฆฌ ์Šคํƒ์—์„œ ํ•œ ๋‹จ๊ณ„ ๋’ค๋กœ ์ด๋™
  • nav('/')์„ ํ˜ธ์ถœํ•˜์—ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ฃจํŠธ ๊ฒฝ๋กœ('/')๋กœ ์ด๋™

 

 


[Board.js]

import { Link, NavLink, Outlet } from "react-router-dom";


function Board() {

  const myStyle = {color : "green", backgroundColor: "red"};
  return (
    <>
    <h3> ๊ธ€ ๋ชฉ๋ก </h3>

    <ul>
      {/* <li><Link to='/board/1'> 1๋ฒˆ๊ธ€ </Link></li>
      <li><Link to='/board/2'> 2๋ฒˆ๊ธ€ </Link></li>
      <li><Link to='/board/3'> 3๋ฒˆ๊ธ€ </Link></li> */}


      <li><NavLink to='/board/1'  style={({isActive}) => isActive ? myStyle : undefined }> 1๋ฒˆ๊ธ€ </NavLink></li>
      <li><NavLink to='/board/2' style={({isActive}) => isActive ? myStyle : undefined }> 2๋ฒˆ๊ธ€ </NavLink></li>
      <li><NavLink to='/board/3' style={({isActive}) => isActive ? myStyle : undefined }> 3๋ฒˆ๊ธ€ </NavLink></li>

    </ul>

{/* ์ค‘์ ‘ ๋ผ์šฐํ„ฐ์—์„œ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ‘œ์‹œํ•จ  */}
    <Outlet/>

    </>
  )
}

export default Board;

 

 

 

 

 

[BoardContent.js]

import { useParams } from "react-router-dom";

function BoardContent() {

  const {num} = useParams();

  return (
    <>
    <h3> ๊ธ€ ๋‚ด์šฉ~ </h3>
    {num}๋ฒˆ ๊ธ€ ์ž…๋‹ˆ๋‹ค. ์ถ”ํ›„ ๋„คํŠธ์›Œํฌ ํ†ต์‹  ์ฒ˜๋ฆฌํ•จ 
    </>
  )
}

export default BoardContent;

 

  • {num} : URL ๊ฒฝ๋กœ์— ํฌํ•จ๋œ ๋งค๊ฐœ๋ณ€์ˆ˜(params)๋ฅผ ์ฝ์–ด์˜ด 

 

style={(isActive)=>{return isActive ? myStyle : undefined ;}}

 

  • NavLink๋Š” ํ˜„์žฌ ํ™œ์„ฑํ™”๋œ ๋งํฌ๋ฅผ ์‰ฝ๊ฒŒ ์Šคํƒ€์ผ๋ง ๊ฐ€๋Šฅ !
  • isActive : NavLink๊ฐ€ ํ™œ์„ฑํ™”๋˜์—ˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ถ€์šธ ๊ฐ’

 

<Outlet/>
  • <Outlet />์€ React Router์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋กœ, ์ค‘์ฒฉ ๋ผ์šฐํŠธ๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ Œ๋”๋งํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ

 

 

 

 


 

[MyPage.js] 

import { Navigate, useNavigate } from "react-router-dom";

function MyPage() {

  let nav = useNavigate();
  let loginYN = false; // ๋กœ๊ทธ์ธ์ฒ˜๋ฆฌ๋‚ด์—ญ๊ด€๋ฆฌ๊ฐ’

  if(!loginYN){
    nav('/');
    return <Navigate to='/' replace={true}/> 
    // ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ๋‚จ๊ธฐ์ง€ ์•Š์Œ
    // ๋’ค๋กœ๊ฐ€๊ธฐ๋ฅผ ๋‚จ๊ธฐ์ง€ ์•Š๋Š”๋‹ค
  }


  return (
    <>
    <h3> ๊ถŒํ•œ ์žˆ๋Š” ์œ ์ €๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ </h3>
    </>
  )
}

export default MyPage;

 

 

 

  if(!loginYN){
    nav('/');
    return <Navigate to='/' replace={true}/> 
  }

 

 

๋กœ๊ทธ์ธ์ด ๋˜์–ด์žˆ์ง€์•Š์œผ๋ฉด

 

 

 

nav('/') 

ํ™ˆ์œผ๋กœ ์ด๋™ํ•˜๊ฒŒ๋œ๋‹ค.