๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
WEB/REACT

[React] BrowserRouter VS HashRouter

by mingzoo 2021. 2. 4.

BrowserRouter VS HashRouter

๊ฐ•์˜์—์„œ๋„ ๊ทธ๋ ‡๊ณ  ์ฑ…์—์„œ๋„ ๊ทธ๋ ‡๊ณ  ๋ผ์šฐํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ด๋•Œ๋Š” ์ด ๋ผ์šฐํ„ฐ๊ฐ€ ์ข€ ๋” ํšจ์œจ์ ์ด๊ณ ,,,์ด๋Ÿฐ ๋ง๋“ค์ด ๋งŽ์•˜๋‹ค. ๋‚˜๋Š” ์ด ์„ค๋ช…๋“ค์ด ํ•œ ๋ฒˆ์— ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ”๊ธฐ ๋•Œ๋ฌธ์— ๋ผ์šฐํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ๋งˆ์Œ์„ ๋จน์—ˆ๋‹ค.

์ผ๋‹จ ์ด ๋ฌด๋น„ ์›น์„ ๋งŒ๋“œ๋Š”๋ฐ ๋น„๊ตํ–ˆ์—ˆ๋˜ ๋ผ์šฐํ„ฐ๋Š” BrowserRouter์™€ HashRouter ์ด ๋‘ ๊ฐœ์ด๋‹ค. ๊ทธ๋ž˜์„œ ์ด ๋‘๊ฐœ๋ฅผ ๋จผ์ € ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•˜๋Š”๋ฐ ์ด ์‚ฌ์ดํŠธ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ์ž‘์„ฑํ•ด๋ณด์•˜๋‹ค.

์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด์„œ ์„ค๋ช…์„ ํ•ด๋ณด๋ ค๊ณ ํ•œ๋‹ค.

 


Router

๋ผ์šฐํ„ฐ์—๋Š” ๊ธฐ๋ณธ์ ์ธ ๋ผ์šฐํŒ… ์†์„ฑ๋“ค๋งŒ ์ •์˜๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์ฒ˜๋Ÿผ 5๊ฐ€์ง€ ๋ผ์šฐํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

  • BrowserRouter
  • HashRouter
  • MemoryRouter
  • NativeRouter
  • StaticRouter

๊ทธ ์ค‘์—์„œ ์˜ค๋Š˜์€ BrowserRouter์™€ HashRouter์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•˜๊ฒ ๋‹ค.

 

BrowserRouter

import React from 'react'; 
import { BrowerRouter, Route, Link } from 'react-touter-dom'; 
import TestChild from './TestChild'; 

const Test () => { 
	return ( 
		<BrowserRouter> 
			<Link to="์ฃผ์†Œ">๋ผ์šฐํ„ฐ๋ณด๊ธฐ</Link> 
			<Route path="./๊ฒฝ๋กœ" component={TestChild}></Route> 
		</BrowserRouter> 
	) 
};

<BrowserRouter></BrowserRouter>๋กœ ๊ฐ์‹ธ์ค˜์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

์ผ๋‹จ <Link to= >์—์„œ = ๋’ค์— ์ด๋™ํ•  ๊ฒฝ๋กœ๋ฅผ ์จ์ค€๋‹ค. ํ•˜์ง€๋งŒ, Link๋Š” ํŽ˜์ด์ง€ ์ด๋™์ด๋ผ๊ธฐ๋ณด๋‹ค ๋ณด์—ฌ์ง€๊ฒŒ ํ‰๋‚ด๋‚ด๋Š” ๊ฒƒ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— Link๋Š” ํŽ˜์ด์ง€์˜ ์ „ํ™˜์„ ๋ฐฉ์ง€ํ•ด์„œ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ Œ๋”๋ง๋˜๋Š” ๋Œ€์ฐธ์‚ฌ๋ฅผ ๋ง‰์•„์ค€๋‹ค.

ํ•˜์ง€๋งŒ, ์ด ์ฃผ์†Œ์—์„œ ์ƒˆ๋กœ๊ณ ์นจ์„ ํ•˜๋ฉด ๊ฒฝ๋กœ์—์„œ ๋ชป ์ฐพ์•„ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์—, BrowserRouter๋Š” History API๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์„œ๋ฒ„์˜ ์ถ”๊ฐ€์ ์ธ ์„ธํŒ…์œผ๋กœ ํ•ด๊ฒฐํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ, BrowserRouter๋Š” ๋™์ ์ธ ํŽ˜์ด์ง€์— ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.

 

www.domain.com/path 

BrowserRouter์‚ฌ์šฉ์‹œ url์€ ์œ„์™€ ๊ฐ™์ด ์ ‘์†ํ•˜๊ฒŒ ๋œ๋‹ค.

 

HashRouter

www.domain.com/#/path 

BrowserRouter์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ ์ฃผ์†Œ ์‚ฌ์ด์— # ํ•ด์‰ฌ๊ฐ’์ด ๋“ค์–ด๊ฐ„๋‹ค.

url์ด ์œ„์™€ ๊ฐ™์ด ์ด๋ฃจ์–ด์ง€๊ฒŒ ๋˜๋Š”๋ฐ #์ด์ „์˜ www.domain.com ์— ๋Œ€ํ•œ ์š”์ฒญ๋งŒ ์„œ๋ฒ„๊ฐ€ ๋ฐ›์•„๋“ค์ด๊ฒŒ ๋˜๋ฏ€๋กœ

์ฃผ์†Œ ์ด๋™ํ›„, ์ƒˆ๋กœ๊ณ ์นจํ•ด๋„ ์—๋Ÿฌ๋ฐœ์ƒ X

๊ฒ€์ƒ‰์—”์ง„์ด ์ฝ์ง€ ๋ชปํ•จ

์„œ๋ฒ„์— ์š”์ฒญ์ด ๊ฐ€์ง€ ์•Š๋Š”๋‹ค -> ๋ฐฑ์—”๋“œ ์„œ๋ฒ„๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๋Š” ์ƒํ™ฉ (์ •์ ์ธ ํŽ˜์ด์ง€)์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋จ

-> ์„œ๋ฒ„๊ฐ€ ์ฃผ์†Œ์˜ ์ด๋™์„ ๋ชจ๋ฅด๊ฒŒ ๋จ (ํŽ˜์ด์ง€ ์กด์žฌ์œ ๋ฌด์— ๋Œ€ํ•ด์„œ ๋ชจ๋ฆ„)

import React from 'react';
import { HashRouter, Route } from "react-router-dom";

function App() {
  return (
    <HashRouter>
      <Navigation />
      <Route path="/" exact={true} component={Home} />
      <Route path="/about" component={About} />
      <Route path="/movie/:id" component={Detail} />
    </HashRouter>
  )
}

์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ <HashRouter></HashRouter>๋กœ ๊ฐ์‹ธ์ค˜์•ผํ•˜๋ฉฐ ์œ„์— ์จ๋†“์€ ๊ฒƒ๊ณผ ๊ฐ™์€ ํŠน์ง•์„ ์ง€๋‹ˆ๊ณ  ์žˆ์–ด ์ •์ ์ธ ํŽ˜์ด์ง€์— ์‚ฌ์šฉ๋œ๋‹ค.

 


๋ญ”๊ฐ€ ์ด์ œ ์ข€ ์ดํ•ด๊ฐ€ ๋˜๋Š”๊ฒƒ๊ฐ™๋‹ค. ๋ญ”๊ฐ€ ๊ฐ์ด ์ข€ ๋œ ์˜ค๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ ๋นจ๋ฆฌ ๋” ๊ณต๋ถ€ํ•ด๋ณด๊ณ  ๋‚ด๊ฐ€ ์ง์ ‘ ๋ชธ์†Œ ๋Š๊ปด๋ณด๊ณ  ์‹ถ๋‹ค. ๊ทผ๋ฐ ๋ญ”๊ฐ€ ์ด๊ฒŒ ์ •ํ™•ํ•œ๊ฐ€ ์˜๋ฌธ์ด ๋“ค์–ด์„œ ํ˜น์‹œ ์ด ๊ธ€์„ ๋ณด์‹œ๋Š” ๋ถ„๋“ค ์ค‘์— ๋ญ”๊ฐ€ ์ž˜๋ชป ๋๋‹ค๊ณ  ๋Š๋ผ์‹œ๋Š” ๋ถ„์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”ใ… 

728x90