whatisthis?

React JS - Movie App project (part 3) ๋ณธ๋ฌธ

PROJECT/WEB

React JS - Movie App project (part 3)

thisisyjin 2022. 2. 19. 15:49
 

React JS - Movie App project (part 2)

{ fet.." data-og-host="mywebproject.tistory.com" data-og-source-url="https://mywebproject.tistory.com/355" data-og-url="https://mywebproject.tistory.com/355" data-og-image="https://scrap.kakaocdn.ne..

mywebproject.tistory.com

๐Ÿ”บ

์ง€๋‚œ์‹œ๊ฐ„ ์ง„ํ–‰์‚ฌํ•ญ

 

- props ์ด์šฉ 

- component ๊ตฌ๋ถ„ (App.js / Movie.js / Home.js / Detail.js)

- ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ตฌ๋ถ„ (components / route)

 

 


 

 

 

1/ ์šฐ์„  React Router์„ ์„ค์น˜ํ•ด์คŒ

npm i react-router-dom@5.3.0

 

2/ App.js์— ์ž„ํฌํŠธํ•จ

 

import { BrowserRouter as Router, Switch, Route } from "react-router-dom";

BrowserRouter์„ ์ž„ํฌํŠธ. (Router, Switch, Route)

>> ๋ˆ„๊ตฐ๊ฐ€ ์ด๋ฏธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค์–ด๋‘” ๊ฒƒ์ž„.

 

 

 

3/ Router, Switch, Route ์ปดํฌ๋„ŒํŠธ ์ด์šฉํ•˜๊ธฐ

return (
    <Router>
      <Switch>
        <Route path="/">
          <Home />
        </Route>
      </Switch>
    </Router>
  );

Router>Switch>Route path="/"> Home(Component)

๐Ÿ”ป

๋ˆ„๊ตฐ๊ฐ€ ์šฐ๋ฆฌ ์›น์‚ฌ์ดํŠธ์˜ "/" url์— ์žˆ๋‹ค๋ฉด

Home์„ ๋ณด์—ฌ์คŒ (Component)

 

 

** Switch๋Š” route๋ฅผ ์ฐพ์•„์ฃผ๋Š” ์—ญํ• ์ž„.

 

 

 

๐Ÿ”ป App.js ์ „์ฒด์ฝ”๋“œ

import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import Home from "./routes/Home";

function App() {
  return (
    <Router>
      <Switch>
        <Route path="/">
          <Home />
        </Route>
      </Switch>
    </Router>
  );
}

export default App;

 


 

์ด์ œ, Route ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€ํ•ด์„œ

path="/movie"์ธ ๊ฒฝ์šฐ์—๋Š” Datail ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์‹คํ–‰๋˜๋„๋ก ํ•ด๋ณด์ž.

 

<Route path="/movie">
  <Detail />
</Route>

 

์ „์ฒด ์ฝ”๋“œ ๐Ÿ”ป

import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import Home from "./routes/Home";
import Detail from "./routes/Detail";

function App() {
  return (
    <Router>
      <Switch>
        <Route path="/movie">
          <Detail />
        </Route>
        <Route path="/">
          <Home />
        </Route>
      </Switch>
    </Router>
  );
}

export default App;

์ฃผ์†Œ์— /movie๋ฅผ ์น˜๋ฉด detail๋กœ ์ด๋™ํ•จ

 

 

๐Ÿ’ก Switch ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋„ฃ์–ด์ค€ ์ด์œ ๋Š”?
ํ•œ๋ฒˆ์— ํ•˜๋‚˜์˜ Route๋งŒ ๋ Œ๋”๋งํ•˜๊ธฐ ์œ„ํ•ด์„œ.

cr: https://reactrouter.com/docs/en/v6/getting-started/tutorial

 

React Router | Tutorial

Declarative routing for React apps at any scale

reactrouter.com

 

 


 

์ด์ œ, Movie.js๋กœ ๋„˜์–ด์™€์„œ

title ๋ถ€๋ถ„์„ ๋ˆ„๋ฅด๋ฉด "/movie"๋งํฌ๋กœ ์ด๋™ํ•˜๋„๋ก ๋งŒ๋“ค์–ด๋ณด์ž.

 

์›๋ž˜๋Œ€๋กœ๋ผ๋ฉด,

<a href="/movie">๋ฅผ ์‚ฌ์šฉํ–ˆ์—ˆ๋‹ค.

 

๋‹จ, aํƒœ๊ทธ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒƒ์€ ํŽ˜์ด์ง€๊ฐ€ ์ƒˆ๋กœ๊ณ ์นจ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋น„ํšจ์œจ์ ์ด๋‹ค.

 

๋‹คํ–‰ํžˆ๋„ refresh์—†์ด๋„ ํŽ˜์ด์ง€ ์ด๋™์ด ๊ฐ€๋Šฅํ•œ Link ์ปดํฌ๋„ŒํŠธ๊ฐ€ react-router-dom์— ์กด์žฌํ•œ๋‹ค!

 

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

์šฐ์„  Link ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ž„ํฌํŠธํ•ด์˜จ๋‹ค.

 

// Movie.js ์ค‘๋žต

<h2>
  <Link to="/movie">{title}</Link>
</h2>

๊ทธ๋ฆฌ๊ณ  h2 ์•ˆ์— Link ์ปดํฌ๋„ŒํŠธ (to = "URL")๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

 

ํŽ˜์ด์ง€ ์ƒˆ๋กœ๊ณ ์นจ ์—†์ด ๋ฐ”๋กœ ์ด๋™ํ•จ!

 

 

 

 

 

+) ์ฐธ๊ณ ๋กœ, Route ์•ˆ์— ์ง์ ‘ html์„ ์ž…๋ ฅํ•ด์ค˜๋„ ๋œ๋‹ค.

<Switch>
    <Route path="/hello">
    	<h1>Hello</h1>
    </Route>
</Switch>

 

 

 

 

 

๐Ÿ– ๋‹ค์Œ step > ๋‹ค์ด๋‚˜๋ฏน(๋™์ ) URL ์ƒ์„ฑ

 

 


 

Ref. 

Free Lecture

 

 

ReactJS๋กœ ์˜ํ™” ์›น ์„œ๋น„์Šค ๋งŒ๋“ค๊ธฐ – ๋…ธ๋งˆ๋“œ ์ฝ”๋” Nomad Coders

React Fundamentals

nomadcoders.co