[ToyProject-Todomate] ํˆฌ๋‘๋ฉ”์ดํŠธ ํด๋ก  ํ”„๋กœ์ ํŠธ 09

2021. 12. 14. 00:13ใ†WEB Dev/ToyProject

728x90


๐Ÿ”ท CloneTodo โ˜‘ - Todomate Clone Project  |  Team CloneMate

 

CloneTodo : ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ์‚ฌ๋ž‘ํ•˜๋Š” ํˆฌ๋‘๋ฉ”์ดํŠธ๋ฅผ ํด๋ก ํ•˜์—ฌ ์›น ์„œ๋น„์Šค๋ฅผ ๋ฐฐํฌํ•ด๋ณด๋Š” ํ”„๋กœ์ ํŠธ

 


 

์–ด์ œ์˜ ์˜ค๋ฅ˜๋Š” useHistory์˜ ๋ฒ„์ „ ๋ฌธ์ œ๋ผ๊ณ  ํ•ด์„œ npm uninstall --save ๋ฅผ ์ด์šฉํ•ด ํŒจํ‚ค์ง€๋ฅผ ์‚ญ์ œํ•˜๊ณ  ๋‹ค์šด๊ทธ๋ ˆ์ด๋“œ๋ฅผ ํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

 

 

npm uninstall --save history@5 react-router-dom@6

 

์œ„์˜ ๋ช…๋ น์–ด๋กœ ์„ค์น˜ํ•œ ๊ฒƒ์„ ์‚ญ์ œํ•˜๊ณ , ๋‹ค์‹œ react-router-dom ์„ ์„ค์น˜ํ•œ๋‹ค.

 

npm install react-router-dom@5

 

๊ทธ๋ฆฌ๊ณ  index.js ํŒŒ์ผ์˜ App ์ปดํฌ๋„ŒํŠธ๋ฅผ <BrowserRouter> </BrowserRouter>๋กœ ๊ฐ์‹ธ์ค€๋‹ค.

(โœ” Strict๋ชจ๋“œ ์ปดํฌ๋„ŒํŠธ๋Š” ์‚ญ์ œํ–ˆ์–ด์š”!)

 

ReactDOM.render(
  <BrowserRouter>
    <App />
  </BrowserRouter>,
  document.getElementById('root')
);

 

๊ทธ ๋‹ค์Œ App.js์— ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํŒจํ‚ค์ง€๋“ค์„ import ํ•ด์˜จ๋‹ค.

์šฐ์„  ์ด๋ ‡๊ฒŒ ์„ธ ๊ฐ€์ง€๋ฅผ import ํ•œ๋‹ค.

 

 

import { Link, Route, Switch } from 'react-router-dom';

 

 

๊ทธ ๋‹ค์Œ์—” ํŽ˜์ด์ง€๊ฐ€ ๋ฐ”๋€Œ์–ด์•ผ ํ•  ๊ณณ์— Route ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.

๋‚˜์˜ ๊ฒฝ์šฐ์—๋Š” ํ™”๋ฉด์˜ ์ผ๋ถ€๊ฐ€ ์•„๋‹Œ ์ „์ฒด๊ฐ€ ์ƒˆ๋กœ์šด ํ™”๋ฉด์œผ๋กœ ๊ต์ฒด๋˜์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— return ๋‹ค์Œ ThemeProvider ์ปดํฌ๋„ŒํŠธ์˜ ๋‹ค์Œ์— Route ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋„ฃ์–ด์คฌ๋‹ค.

 

Route ์˜ ์ฒซ๋ฒˆ์งธ๋Š” /๋กœ App ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก 

 

<Route exact path="/">
  <div className="App">
    <div className="header">
      <img className="main_img" src="images/todomate.jpg" />
      <h1>todo mate</h1>
      <h3>ํ•  ์ผ์„ ์ž‘์„ฑ, ๊ณ„ํš, ๊ด€๋ฆฌํ•˜์„ธ์š”.</h3>
     </div>
     <div className="start_btn">
      <Button className="join_btn" variant="contained" color="btn">
      ๊ฐ€์ž…ํ•˜๊ธฐ
      </Button>
      <Button className="signin_btn" color="btn" variant="contained">
      ๋กœ๊ทธ์ธ
      </Button>
    </div>
  </div>
</Route>

 

์œ„์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์งœ ์ฃผ๊ณ ,

/signin path์—๋Š” Signin ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.

 

<Route exact path="/signin">
	<Signin />
</Route>

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋กœ์ปฌ ์ฃผ์†Œ /signin์„ ํ–ˆ์„ ๋•Œ ์ €๋ฒˆ์— Signin ์ปดํฌ๋„ŒํŠธ์— ๋„ฃ์–ด๋‘” MUI FormControl ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.

๊ทธ๋Ÿผ ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ /signin์œผ๋กœ ๋„˜์–ด๊ฐ€๊ฒŒ ํ•˜๋ ค๋ฉด?

 

 

 

์ด์ œ useHistory๋ฅผ ์จ๋ณด์ž!

 

 

App.js ํŒŒ์ผ์— useHistory๋ฅผ import ํ•œ๋‹ค.

 

import { useHistory } from 'react-router-dom';
// ์ด๋ฏธ react-router-dom์„ ์ด์šฉํ•ด์„œ Route๋ฅผ import ํ•˜๊ณ  ์žˆ์–ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ๋‹ค.
// import { Route, useHistory } from "react-router-dom";

 

 

 

๊ทธ๋ฆฌ๊ณ  App( ) ํ•จ์ˆ˜ ์•ˆ์— let history = useHistory();๋ฅผ ๋„ฃ์–ด hook ์ฒ˜๋Ÿผ useHistory()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

Button ์ปดํฌ๋„ŒํŠธ ์ค‘ ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์— onClick์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ๋‚ด๋ถ€์— ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  history ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

 

<Button className="signin_btn" color="btn" variant="contained" onClick={()=>{ history.push('/signin') }}>

 

 

๋ผ์šฐํ„ฐ ์™„์„ฑ~!

 

์ธ์ค„ ์•Œ์•˜์ง€๋งŒ "/signin"์œผ๋กœ ์ ‘์†ํ•˜๋ฉด "/" ํŽ˜์ด์ง€ ํ•˜๋‹จ์— ํ‘œ์‹œ๋˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Switch ๋ฌธ๋ฒ•์„ ํ†ตํ•ด ํ•ด๋‹น ํ™”๋ฉด๋งŒ ํ‘œ์‹œ๋˜๋„๋ก ๋งŒ๋“ค์–ด ์ค€๋‹ค.

Route ์ปดํฌ๋„ŒํŠธ ๋ฐ–์— <Switch></Switch> ๋กœ ๊ฐ์‹ธ์ฃผ๋ฉด ๋œ๋‹ค.

 

 

์ตœ์ข… ์ฝ”๋“œ๋Š” ์ด๋ ‡๋‹ค.

 

import React from "react";
import "../stylesheets/App.css";
import { Button  } from "@mui/material";
import { ThemeProvider, createTheme } from "@mui/material/styles";
import { Switch, Route, useHistory } from "react-router-dom";

import Signin from "./Signin";

const theme = createTheme({
  status: {
    danger: "#e53e3e",
  },
  palette: {
    primary: {
      main: "#0971f1",
      darker: "#053e85",
    },
    neutral: {
      main: "#64748B",
      contrastText: "#fff",
    },
    btn: {
      main: "#f2f2f2",
      contrastText: "#111",
    },
  },
});

function App() {
  let history = useHistory();

  return (
    <ThemeProvider theme={theme}>
      <Switch>
        <Route exact path="/">
          <div className="App">
            <div className="header">
              <img className="main_img" src="images/todomate.jpg" />
              <h1>todo mate</h1>
              <h3>ํ•  ์ผ์„ ์ž‘์„ฑ, ๊ณ„ํš, ๊ด€๋ฆฌํ•˜์„ธ์š”.</h3>
            </div>
            <div className="start_btn">
              <Button className="join_btn" variant="contained" color="btn">
                ๊ฐ€์ž…ํ•˜๊ธฐ
              </Button>
              <Button
                className="signin_btn"
                color="btn"
                variant="contained"
                onClick={() => {
                  history.push("/signin");
                }}
              >
                ๋กœ๊ทธ์ธ
              </Button>
            </div>
          </div>
        </Route>

        <Route exact path="/signin">
          <Signin />
        </Route>
        </Switch>

    </ThemeProvider>
  );
}

export default App;

 

 

 

728x90