// data.js ํ์ผ
let data = [
{
id : 0,
title : "White and Black",
content : "Born in France",
price : 120000
},
{
id : 1,
title : "Red Knit",
content : "Born in Seoul",
price : 110000
},
{
id : 2,
title : "Grey Yordan",
content : "Born in the States",
price : 130000
}
] ;
export default data
// App.js ํ์ผ
import data from './data.js';
function App(){
let [shoes] = useState(data);
return (
<div className="App">
(์๋ต)
<div className="container">
<div className="row">
<div className="col-md-4">
<img src="https://codingapple1.github.io/shop/shoes1.jpg" width="80%" />
<h4>{ shoes[0].title }</h4>
<p>{ shoes[0].price }</p>
</div>
<div className="col-md-4">
<img src="https://codingapple1.github.io/shop/shoes2.jpg" width="80%" />
<h4>์ํ๋ช
</h4>
<p>์ํ์ ๋ณด</p>
</div>
<div className="col-md-4">
<img src="https://codingapple1.github.io/shop/shoes3.jpg" width="80%" />
<h4>์ํ๋ช
</h4>
<p>์ํ์ ๋ณด</p>
</div>
</div>
</div>
</div>
)
}
}
๐โ๏ธ html ๊ธด ๋ถ๋ถ ์ปดํฌ๋ํธ๋ก ๋ง๋ค์ด๋ณด๊ธฐ
function App(){
return (
<div className="App">
(์๋ต)
<div className="container">
<div className="row">
<Card/>
<Card/>
<Card/>
</div>
</div>
</div>
)
}
function Card(){
return (
<div className="col-md-4">
<img src="https://codingapple1.github.io/shop/shoes1.jpg" width="80%" />
<h4>์ํ๋ช
</h4>
<p>์ํ์ ๋ณด</p>
</div>
)
}
๐โ๏ธ shoes์ ์๋ ๊ฑฐ Card ์ปดํฌ๋ํธ ์์ ๊ฝ์๋ฃ๊ธฐ
// function App ๋ด๋ถ
<Card shoes={shoes[0]} i={1} />
<Card shoes={shoes[1]} i={2} />
<Card shoes={shoes[2]} i={3} />
function Card(props){
return (
<div className="col-md-4">
<img src={'https://codingapple1.github.io/shop/shoes' + props.i + '.jpg'} width="80%" />
<h4>{ props.shoes.title }</h4>
<p>{ props.shoes.price }</p>
</div>
)
}
๐โ๏ธ Detail ์ปดํฌ๋ํธ ๋ง๋ค๊ธฐ
function Detail(){
return (
<div className="container">
<div className="row">
<div className="col-md-6">
<img src="https://codingapple1.github.io/shop/shoes1.jpg" width="100%" />
</div>
<div className="col-md-6">
<h4 className="pt-5">์ํ๋ช
</h4>
<p>์ํ์ค๋ช
</p>
<p>120000์</p>
<button className="btn btn-danger">์ฃผ๋ฌธํ๊ธฐ</button>
</div>
</div>
</div>
)
}
export default Detail
// App.js ์์ ํ์ฉ
import Detail from './routes/Detail.js'
function App(){
return (
(์๋ต)
<Route path="/detail" element={ <Detail/> } />
)
}
โ๏ธ ๋ฆฌ์กํธ ๋ผ์ฐํฐ
โ๏ธ react-router-dom ์ค์น
- ํฐ๋ฏธ๋ ์ด์ด์ npm install react-router-dom@6 ์
๋ ฅํด์ ์ค์น
- ์
ํ
์ index.js ํ์ผ๋ก ๊ฐ์ import~ gkrh <BrowserRouter> ์ด๊ฑธ๋ก <App />์ ๊ฐ์ผ๋ค.
import { BrowserRouter } from "react-router-dom";
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>
);
โ๏ธ ๋ผ์ฐํฐ๋ก ํ์ด์ง ๋๋๋ ๋ฒ
- url ๊ฒฝ๋ก๋ง๋ค ๋ค๋ฅธ ํ์ด์ง๋ฅผ ๋ณด์ฌ์ฃผ๊ณ ์ถ์ผ๋ฉด ์๋์ ๊ฐ์ด ์์ฑ
// App.js
import { Routes, Route, Link } from 'react-router-dom'
function App(){
return (
(์๋ต)
<Routes>
<Route path="/" element={
<>
<div className="main-bg"></div>
<div className="container">
<div className="row">
{ shoes.map((a, i)=>{
return <Card shoes={shoes[i]} i={i} ></Card>
})}
</div>
</div>
</>
} />
<Route path="/detail" element={ <div>์์ธํ์ด์ง์</div> } />
<Route path="/about" element={ <div>์ด๋ฐ์ํ์ด์ง์</div> } />
</Routes>
)
}
1. ์ฐ์ ์๋จ์์ ์ฌ๋ฌ๊ฐ์ง ์ปดํฌ๋ํธ import ํด์ค๊ณ
2. <Routes> ๋ง๋ค๊ณ ๊ทธ ์์ <Route> ์์ฑ
3. <Route path="/๊ฒฝ๋ก" element={ <๋ณด์ฌ์คhtml> } /> ์์ฑ
โ๏ธ ํ์ด์ง ์ด๋ ๋ฒํผ
- ๋งํฌ๋ฅผ ํด๋ฆญํด ์ด๋
import { Link } from 'react-router-dom';
<Link to="/">ํ</Link>
<Link to="/detail">์์ธํ์ด์ง</Link>
โ๏ธ ํ์ด์ง ์ด๋๊ธฐ๋ฅ ๋ง๋ค๊ธฐ: usenavigate()
- Link ์ฐ๋ฉด ๋ชป์๊ฒผ์
import { Routes, Route, Link, useNavigate, Outlet } from 'react-router-dom'
function App(){
let navigate = useNavigate()
return (
(์๋ต)
<button onClick={()=>{ navigate('/detail') }}>์ด๋๋ฒํผ</button>
)
}
- -1 ๋ฃ์ผ๋ฉด ๋ค๋ก 1๋ฒ ๊ฐ๊ธฐ, 2 ๋ฃ์ผ๋ฉด ์์ผ๋ก 2๋ฒ ๊ฐ๊ธฐ ๊ธฐ๋ฅ
โ๏ธ 404 ํจ์ด์ง
<Route path="*" element={ <div>์๋ํ์ด์ง์</div> } />
- * ๊ฒฝ๋ก๋ ๋ชจ๋ ๊ฒฝ๋ก๋ฅผ ๋ปํด์ ์์ ๋ง๋ค์ด๋ /detail ์ด๋ฐ๊ฒ ์๋ ์ด์ํ ํ์ด์ง ์ ์์ * ๊ฒฝ๋ก๋ก ์๋ด
โ๏ธ ์๋ธ๊ฒฝ๋ก ๋ง๋ค ์ ์๋ nested routes
<Route path="/about" element={ <About/> } >
<Route path="member" element={ <div>๋ฉค๋ฒ๋ค</div> } />
<Route path="location" element={ <div>ํ์ฌ์์น</div> } />
</Route>
- <Route> ์์ <Route> ๋ฅผ ๋ฃ๋ ๊ฒ์ Nested routes ๋ผ๊ณ ๋ถ๋ฅธ๋ค.
- ์์ ๊ฐ์ด ์ฌ์ฉํ๋ฉด /about/member๋ก ์ ์์ <About> & <div>๋ฉค๋ฒ๋ค></div>์ ๋ณด์ฌ์ฃผ๊ณ
- /about/location์ผ๋ก ์ ์์ <About> & <div>ํ์ฌ์์น</div> ์ ๋ณด์ฌ์ค๋ค.
- ์ค์ ๋ก ์ ์ฝ๋๋ /about/memeber๋ก ์ ์์ <About>์์ <div>๋ฉค๋ฒ๋ค</div>์ ๋ณด์ฌ์ค๋ค.
- <About> ์ปดํฌ๋ํธ ์์ <div>๋ฅผ ์ด๋๋ค ๋ณด์ฌ์ค์ง ํ๊ธฐํด์ผ ์ ๋ณด์ฌ์ค
function About(){
return (
<div>
<h4>aboutํ์ด์ง์</h4>
<Outlet></Outlet> <!-- nested routes ์์ element๋ค์ ์ด๋์ ๋ณด์ฌ์ค์ง ํ๊ธฐํ๋ ๊ณณ -->
</div>
)
}
โ๏ธ ์์ธํ์ด์ง์ ์ํ๋ช
๋ฃ๊ธฐ
- shoes๋ผ๋ state์ ์๋ ์ํ ์ ๋ณด๋ค์ Detail ์ปดํฌ๋ํธ์ ๋ฃ์ด๋ณด์.
// App.js
<Route path="/detail" element={ <Detail shoes={shoes}/> }/> // <Detail> ์ฐ๋ ๊ณณ์์ ์ผ๋จ props ์ ์ก
// Detail.js
<div className="container>
<div className="row">
<div className="col-md-6">
<img src="https://codingapple1.github.io/shop/shoes1.jpg" width="100%" />
</div>
<div className="col-md-6 mt-4">
<h4 className="pt-5">{props.shoes[0].title}</h4> <!-- 0๋ฒ์งธ ์ํ -->
<p>{props.shoes[0].content}</p>
<p>{props.shoes[0].price}์</p>
<button className="btn btn-danger">์ฃผ๋ฌธํ๊ธฐ</button>
</div>
</div>
</div>
โ๏ธ ์์ธํ์ด์ง ์ฌ๋ฌ๊ฐ ๋ง๋ค๊ธฐ
<Route path="/detail/:id" element={ <Detail shoes={shoes}/> }/> <!-- url ํ๋ผ๋ฏธํฐ ์ฌ์ฉ -->
<!-- ๊ฒฐ๊ณผ: /detail/0 /detail/1 /detail/2 -->
- path ์๋ช
ํ ๋ /:์ด์ฉ๊ตฌ ์ด๋ ๊ฒ ์ฌ์ฉํ๋ฉด "์๋ฌด ๋ฌธ์"๋ฅผ ๋ปํ๋ค.
- ๊ทธ๋์ ์์ <Route>๋ ๋๊ตฐ๊ฐ ์ฃผ์์ฐฝ์ /detail/์๋ฌด๊ฑฐ๋ ์
๋ ฅํ์ ๋ <Detail> ์ปดํฌ๋ํธ๋ฅผ ๋ณด์ฌ๋ฌ๋ผ๋ ๋ป
โ๏ธ ํ์ด์ง๋ง๋ค ๋ค๋ฅธ ๋ด์ฉ ๋ณด์ฌ์ฃผ๊ธฐ
import { useParams } from 'react-router-dom'
function Detail(){
let {id} = useParams(); // /:url ํ๋ผ๋ฏธํฐ ์๋ฆฌ๊ฐ ์ ์ ๊ฐ ์
๋ ฅํ ๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์์
console.log(id)
return (
<div className="container>
<div className="row">
<div className="col-md-6">
<img src="https://codingapple1.github.io/shop/shoes1.jpg" width="100%" />
</div>
<div className="col-md-6 mt-4">
<h4 className="pt-5">{props.shoes[id].title}</h4>
<p>{props.shoes[0].content}</p>
<p>{props.shoes[0].price}์</p>
<button className="btn btn-danger">์ฃผ๋ฌธํ๊ธฐ</button>
</div>
</div>
</div>
)
}
๋ณธ ํฌ์คํ ์ << React ๋ฆฌ์กํธ ๊ธฐ์ด๋ถํฐ ์ผํ๋ชฐ ํ๋ก์ ํธ๊น์ง! >> ๊ฐ์๋ฅผ ์ฐธ๊ณ ํฉ๋๋ค.