[TIL]21.06.08

๋ฐ•์ฃผํ™ยท2021๋…„ 6์›” 8์ผ
0

Today I Learned

๋ชฉ๋ก ๋ณด๊ธฐ
31/104

๐Ÿ‘จโ€๐Ÿ’ป ์˜ค๋Š˜ ๊ณต๋ถ€ํ•œ ๊ฒƒ

  1. ๋ฆฌ์•กํŠธ์—์„œ..
    import {BrowserRouter as Router} from 'react-router-dom' ์œผ๋กœ BrowserRouter ๋Œ€์‹  Router๋กœ ์“ธ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑธ ๋ฐฐ์› ๋‹ค.

  2. 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>
  
}
  1. useParams
    http://localhost:3000/about/:id
    about์ด๋ž€ path๋’ค์— id๊ฐ’์— ๋”ฐ๋ผ ๋ Œ๋”๋ง์„ ๋‹ค๋ฅด๊ฒŒ ํ•˜๊ณ ์‹ถ์„ ๋•Œ useParams๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๊ฒŒ ์กฐ๊ฑด๋ถ€๋ Œ๋”๋ง์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.

const {picture, username, id, content} = tweet;

๋งŒ์•ฝ ํ•˜์œ„์ปดํฌ๋„ŒํŠธ์—์„œ tweet์ด๋ž€ ์ด๋ฆ„์œผ๋กœ ๊ฐ์ฒดํ˜•ํƒœ์˜ props์„ ๋ฐ›์•„์˜จ๋‹ค๋ฉด ์ด๋Ÿฐ์‹์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. picture, username, id, content์ด ๊ฒƒ๋“ค์„ ๋ณ€์ˆ˜์ด๋ฆ„์ฒ˜๋Ÿผ, ๋ณ€์ˆ˜์ฒ˜๋Ÿผ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š์œผ๋ฉด, {tweet.id} ์ด๋Ÿฐ์‹์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค.


์˜ค๋Š˜ ๋‚˜๋ฆ„ ์—ด์‹ฌํžˆ ํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค. ์‚ฝ์งˆ์„ ์ˆ˜๋„์—†์ด ํ•œ๊ฑธ๋ณด๋ฉด... ใ…Žใ…Ž
ํ•˜์ง€๋งŒ ๋‚˜๋ณด๋‹ค ์ž˜ํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์ด ๋‚˜๋ณด๋‹ค ๋” ์ปดํ“จํ„ฐ์•ž์— ์˜ค๋ž˜ ์•‰์•„์žˆ๋Š” ๊ฑธ ๋ณด๊ณ  ๋ฐ˜์„ฑํ•˜๊ฒŒ๋๋‹ค. ์•„๋ฌด๋ฆฌ๋ชปํ•ด๋„ ๊ทธ๋“ค๋ณด๋‹ค๋Š” ๋” ๋งŽ์ด ํ•ด์•ผํ•  ๊ฒƒ์ด๋‹ค.
profile
๊ณ ํ†ต์—†๋Š” ์„ฑ์žฅ์€ ์—†๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค....

0๊ฐœ์˜ ๋Œ“๊ธ€