๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“— React

[React ] React Router๋ฅผ ์ด์šฉํ•˜์—ฌ component๊ฐ„์— props ๋„˜๊ฒจ์ฃผ๊ธฐ ( (Link & props)

by Tamii 2021. 3. 18.
๋ฐ˜์‘ํ˜•

react ๋ฅผ ์ด์šฉํ•˜์—ฌ navigation bar๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค์–ด๋ณด์ž

navigation bar๋ฅผ ๋งŒ๋“ค์–ด๋ณด๋ฉด์„œ  reactd์—์„œ๋Š” ํŽ˜์ด์ง€ ๊ฐ„์— data๋ฅผ ์–ด๋–ป๊ฒŒ ์—ฐ๊ฒฐํ•˜๊ณ  ๊ณต์œ ํ•˜๋Š”์ง€ ๊ณต๋ถ€ํ•ด ๋ณผ ๊ฒƒ์ด๋‹ค.

 

1) react-router-dom ์„ค์น˜

* npm ์œผ๋กœ ์„ค์น˜

npm install react-router-dom

* yarn ์œผ๋กœ ์„ค์น˜

yarn add react-router-dom

2) Routes ํด๋” ์ƒ์„ฑ

routes ํด๋”์—๋Š” ๋‚ด๊ฐ€ ๋งŒ๋“ค๊ณ  ์‹ถ์€(์ด๋™ํ• ) ํŽ˜์ด์ง€ ๊ฐœ์ˆ˜๋งŒํผ ๋งŒ๋“ ๋‹ค

 

routes์— 3๊ฐœ์˜ ํŽ˜์ด์ง€ ์ƒ์„ฑ

 

- Home.js  : ๋ฉ”์ธ ์˜ํ™”์„ค๋ช… (์˜ํ™”๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ํŽ˜์ด์ง€ )

- About.js  : ์›นํŽ˜์ด์ง€ ์†Œ๊ฐœ๊ธ€ (์ž๊ธฐ์†Œ๊ฐœ ๋„ฃ์„ ์˜ˆ์ •)

- Datail.js  :  ์˜ํ™” ์ƒ์„ธ ์„ค๋ช… (์˜ํ™” ํด๋ฆญ์‹œ ์ƒ์„ธ์„ค๋ช… ํŽ˜์ด์ง€)

 

 

 

 

 

 

 

 

 

 

์ผ๋‹จ ํ˜„์žฌ๋Š”

APP.js์—์„œ Movies๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ํŒŒ์‹ฑํ•˜๋Š” ์ผ์„ ์ „๋ถ€ ํ•˜๋Š” ์ƒํ™ฉ 

 

APP.js ์—์„œ getMovie ๋กœ ๊ฐ€์ ธ์™€์„œ ์ „๋ถ€ ๋‹ค ๊ตฌํ˜„ํ•˜๋˜ ๋ฐฉ์‹์„ 

getMovie -> routesํŒŒ์ผ๋กœ ๋‚˜๋ˆ ์„œ๊ตฌํ˜„ํ•  ์˜ˆ์ • 


๋ผ์šฐํ„ฐ

url์„ ์ด์šฉํ•˜์—ฌ path๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์ž‡์Œ 

url์„ ๋งค์น˜ํ•˜๋ฉฐ ํ•ด๋‹นํ•˜๋Š” ๋ผ์šฐํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€ ๋ณด์—ฌ์ค€๋‹ค

ex) 

 

/home/

/home/introduction

์ด๋ ‡๊ฒŒ 2๊ฐœ์˜ ๋ผ์šฐํ„ฐ๊ฐ€ ์žˆ์œผ๋ฉด 2 ๋ผ์šฐํ„ฐ๋ฅผ ๋‹ค ๋ Œ๋”๋ง ํ•˜๊ธฐ๋•Œ๋ฌธ์—

ํ•œ ํŽ˜์ด์ง€์—์„œ ๋‘๊ฐœ๊ฐ€ ๊ฒน์ณ ๋ณด์ด๊ฒŒ ๋œ๋‹ค.(2๊ฐœ์˜ component๋ฅผ ๋ณด์—ฌ์คŒ)

 

/home/ : /home/ ๊ณผ /home/introduction์ด ๊ณต์กด

/home/introduction : /home/introdunction/ ๋งŒ ๋ Œ๋”๋ง 

 

 

exact ์ด์šฉํ•˜๊ธฐ

exact ={true}๋ฅผ ๋„ฃ๊ฒŒ ๋˜๋ฉด path๊ฐ€ ์˜ค์ง "/"์ผ๋•Œ๋งŒ ๋™์ž‘ํ•˜๋„๋ก ์‹คํ–‰๋จ 

import React from "react";
import { HashRouter ,Route} from "react-router-dom";
import Home from "./routes/Home";
import About from "./routes/About";

function App() {
  return (
    <HashRouter>
      <Route path="/" exact={true} component={Home} />
      <Route path="/about" component={About} />
    </HashRouter>
  );
}

export default App;

 


Props

๋ชจ๋“  Route ์•ˆ์— ์žˆ๋Š” ๋ผ์šฐํŠธ๋“ค์€ ๊ธฐ๋ณธ props ๊ฐ’์„ ๊ฐ€์ง‘

Ex)

์—ฌ๊ธฐ์„œ๋Š”  Home, About, Detail ์—์„œ 

props ๊ฐ’์„ ์ด์šฉํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

 

 


Link

React์—์„œ๋Š” ๋งํฌ๋ฅผ ๊ฑธ์–ด์ค„๋•Œ <a> tage ๋Œ€์‹  <Link>๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค.

a tag๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ๋งํฌ๋ฅผ ๋ˆ„๋ฅผ๋•Œ๋งˆ๋‹ค ์žฌ ๋ Œ๋”๋ง ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์›นํŽ˜์ด์ง€๊ฐ€ ์ƒˆ๋กœ๊ณ ์นจ ๋˜๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

<Link> ๋Š” ๋งํฌ๋ฅผ ์—ฐ๊ฒฐ์‹œ์ผœ์ค„ ๋ฟ ์•„๋‹ˆ๋ผ object ์ „๋‹ฌ์ด ๊ฐ€๋Šฅํ•จ 

<Link to={{
            pathname: `/movie/${id}`,
            state: {
                year:year,
                title:title,
                summary:summary,
                poster:poster,
                genres:genres
            }
          }}>

์ด๋Ÿฐ์‹์œผ๋กœ path ์™€ state๋ฅผ ์ง€์ •ํ•˜์—ฌ ๋งํฌ์—ฐ๊ฒฐ, ์†์„ฑ(props) ์œผ๋กœ ์ „๋‹ฌ 

 

 

 

์ „๋‹ฌํ•œ ์†์„ฑ(props)๋ฅผ ๋ฐ›์•„ ์›ํ•˜๋Š”๋Œ€๋กœ  render ํ•˜์—ฌ ์‚ฌ์šฉ ๊ฐ€๋Šฅ 

 

class Detail extends React.Component{
    
    componentDidMount() {
        const { location,history } = this.props;
        if (location.state === undefined) {
            history.push("/");
        }
    }

    render() {
        console.log(this.props);
        

        const { location } = this.props;
        if (location.state) {
            return (
                <div className="detail__container">
                    <h1>๐ŸŽž Movie details</h1>
                    <span>{location.state.title}</span>
                    <span>{location.state.summary}</span>
                </div>
            );
        }
        else {
            return null;
        }
    }
}

 

 

 

 

๋Œ“๊ธ€