๐จโ๐ป ์ค๋ ๊ณต๋ถํ ๊ฒ
๋ฆฌ์กํธ์์..
import {BrowserRouter as Router} from 'react-router-dom' ์ผ๋ก BrowserRouter ๋์ Router๋ก ์ธ ์ ์๋ค๋ ๊ฑธ ๋ฐฐ์ ๋ค.
useHistory
๋ค์๊ณผ ๊ฐ์ด history๊ฐ path๋ฅผ push๋ฐ๊ณ ๊ธฐ์ตํด ๋ค๋ก๊ฐ๊ธฐ ๊ธฐ๋ฅ์ ์ฝ๊ฒ ๊ตฌํํ ์ ์๋ค.
import { useHistory, BrowserRouter, Switch, Route, Link } from 'react-router-dom';
const App = () => {
let history = useHistory('/');
const handleHistory = () =>{
if(e.target.className === 'far fa-comment-dots'){
history.push('/');
}else if(e.target.className === 'far fa-question-circle'){
history.push('/about');
}else if(e.target.className === 'far fa-user'){
history.push('/mypage');
}
}
<BrowserRouter>
<Link onClick={handleHistory} to="/"><i className="far fa-comment-dots"></i></Link>
<Link onClick={handleHistory} to="/about"><i className="far fa-question-circle"></i></Link>
<Link onClick={handleHistory} to="/mypage"><i className="far fa-user"></i></Link>
<Switch>
<Route exact path="/"><Home /></Route>
<Route path="/about"><About /></Route>
<Route path="/mypage"><MyPage /></Route>
</Switch>
</BrowserRouter>
}
http://localhost:3000/about/:id
const {picture, username, id, content} = tweet;
๋ง์ฝ ํ์์ปดํฌ๋ํธ์์ tweet์ด๋ ์ด๋ฆ์ผ๋ก ๊ฐ์ฒดํํ์ props์ ๋ฐ์์จ๋ค๋ฉด ์ด๋ฐ์์ผ๋ก ์ฌ์ฉ ๊ฐ๋ฅํ๋ค. picture, username, id, content
์ด ๊ฒ๋ค์ ๋ณ์์ด๋ฆ์ฒ๋ผ, ๋ณ์์ฒ๋ผ ์ฌ์ฉ๊ฐ๋ฅํ๋ค. ์ด๋ ๊ฒ ํ์ง ์์ผ๋ฉด, {tweet.id}
์ด๋ฐ์์ผ๋ก ์ฌ์ฉํด์ผ ํ ๊ฒ์ด๋ค.