๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ฌ/ใ…ใ……ใ…Œใ…‹ใ…ใ…… ์ฑŒ๋ฆฐ์ง€

35์ผ์ฐจ

๐Ÿฆฆ

35์ผ์ฐจ

 

Part 10. React ๋กœ ์‡ผํ•‘๋ชฐ ๋งŒ๋“ค๊ธฐ (React ๊ธฐ๋ณธ)

Ch 4. React Router

 


 

 

Ch 4. React Router

 

 

https://reactrouter.com/

 

Declarative routing for React apps at any scale | React Router

Version 6 of React Router is here! React Router v6 takes the best features from v3, v5, and its sister project, Reach Router, in our smallest and most powerful package yet.

reactrouter.com

 

$ npm i react-router-dom
// App.js

import { BrowserRouter, Route, Routes } from "react-router-dom";
import About from "./pages/About";
import Home from "./pages/Home";
import Profile from "./pages/Profile";

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/profile" element={<Profile />} />
        <Route path="/about" element={<About />} />
      </Routes>
    </BrowserRouter>
  );
}

export default App;

 

 

 ๐Ÿ”— Dynamic ๋ผ์šฐํŒ… 

 

/profile/1

// App.js

import { BrowserRouter, Route, Routes } from "react-router-dom";
import About from "./pages/About";
import Home from "./pages/Home";
import Profile from "./pages/Profile";

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/profile/*" element={<Profile />}>
          <Route path=":id" element={<Profile />} />
        </Route>
        <Route path="/about" element={<About />} />
      </Routes>
    </BrowserRouter>
  );
}

export default App;
// Profile.jsx

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

export default function Profile() {
  const params = useParams();
  const id = params.id;
  console.log(id, typeof id);

  return (
    <div>
      <h2>Profile ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.</h2>
      {id && <p>id๋Š” {id} ์ž…๋‹ˆ๋‹ค.</p>}
    </div>
  );
}

 

/about?name=mark

https://developer.mozilla.org/ko/docs/Web/API/URLSearchParams

 

URLSearchParams - Web API | MDN

URLSearchParams ์ธํ„ฐํŽ˜์ด์Šค๋Š” URL์˜ ์ฟผ๋ฆฌ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

developer.mozilla.org

https://github.com/sindresorhus/query-string

 

GitHub - sindresorhus/query-string: Parse and stringify URL query strings

Parse and stringify URL query strings. Contribute to sindresorhus/query-string development by creating an account on GitHub.

github.com

$ npm i query-string
// About.jsx

import { useLocation } from "react-router-dom";
import queryString from "query-string";

export default function About(props) {
  const location = useLocation();
  const searchParams = location.search;
  console.log(searchParams);

  // const obj = new URLSearchParams(searchParams);
  // console.log(obj.get("name"));

  const query = queryString.parse(searchParams);
  console.log(query);

  return (
    <div>
      <h2>About ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.</h2>
      {query.name && <p>name ์€ {query.name} ์ž…๋‹ˆ๋‹ค.</p>}
    </div>
  );
}

 

 

 ๐Ÿ”— Switch์™€ NotFound  => v6์—์„œ Switch ์‚ฌ๋ผ์ง !!!!!!

 

import { BrowserRouter, Route, Routes } from "react-router-dom";
import About from "./pages/About";
import Home from "./pages/Home";
import NotFound from "./pages/NotFound";
import Profile from "./pages/Profile";

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/profile/*" element={<Profile />}>
          <Route path=":id" element={<Profile />} />
        </Route>
        <Route path="/about/*" element={<About />} />
        <Route path="/*" element={<NotFound />}></Route>
      </Routes>
    </BrowserRouter>
  );
}

export default App;

 

 

 ๐Ÿ”— JSX ๋งํฌ๋กœ ๋ผ์šฐํŒ… ์ด๋™ํ•˜๊ธฐ 

 

<NavLink></NavLink>
active ์ƒํƒœ์— ๋Œ€ํ•œ ์Šคํƒ€์ผ ์ง€์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

<Links /> ์ฐพ์„ ์ˆ˜ ์—†๋‹ค๊ณ  ๋– ์„œ ์ž„์‹œ๋ฐฉํŽธ์œผ๋กœ ๋ณธ๋ฌธ์— ์ง์ ‘ ์ฝ”๋“œ ๋„ฃ์—ˆ์Œ;;;

// App.js

import { BrowserRouter, Route, Routes, Link } from "react-router-dom";
// import Links from "./components/Links";
import NavLinks from "./components/NavLinks";
import About from "./pages/About";
import Home from "./pages/Home";
import NotFound from "./pages/NotFound";
import Profile from "./pages/Profile";

function App() {
  return (
    <BrowserRouter>
      {/* <Links /> */}
      <ul>
        <li>
          <Link to="/">Home</Link>
        </li>
        <li>
          <Link to="/profile">Profile</Link>
        </li>
        <li>
          <Link to="/profile/1">Profile/1</Link>
        </li>
        <li>
          <Link to="/about">About</Link>
        </li>
        <li>
          <Link to="/about?name=mark">About?name=mark</Link>
        </li>
      </ul>
      <NavLinks />
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/profile/*" element={<Profile />}>
          <Route path=":id" element={<Profile />} />
        </Route>
        <Route path="/about/*" element={<About />} />
        <Route path="/*" element={<NotFound />}></Route>
      </Routes>
    </BrowserRouter>
  );
}

export default App;
// NavLinks.jsx

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

const activeStyle = { color: "green" };

export default function NavLinks() {
  return (
    <ul>
      <li>
        <NavLink
          to="/"
          // end
          style={({ isActive }) => (isActive ? activeStyle : undefined)}
        >
          Home
        </NavLink>
      </li>
      <li>
        <NavLink
          to="/profile"
          end
          style={({ isActive }) => (isActive ? activeStyle : undefined)}
        >
          Profile
        </NavLink>
      </li>
      <li>
        <NavLink
          to="/profile/1"
          style={({ isActive }) => (isActive ? activeStyle : undefined)}
        >
          Profile/1
        </NavLink>
      </li>
      <li>
        <NavLink
          to="/about"
          style={({ isActive }) => (isActive ? activeStyle : undefined)}
          // isActive={(match, location) => {
          //   console.log(location);
          //   return location.search === "";
          // }}
        >
          About
        </NavLink>
      </li>
      <li>
        <NavLink
          to="/about?name=mark"
          style={({ isActive }) => (isActive ? activeStyle : undefined)}
          // isActive={(match, location) => {
          //   console.log(location);
          //   return location.search === "";
          // }}
        >
          About?name=mark
        </NavLink>
      </li>
    </ul>
  );
}

 

 

 ๐Ÿ”— JS ๋กœ ๋ผ์šฐํŒ… ์ด๋™ํ•˜๊ธฐ 

 

// Login.jsx

import LoginButton from "../components/LoginButton";

export default function Login() {
  return (
    <div>
      <h2>Login ํŽ˜์ด์ง€ ์ž…๋‹ˆ๋‹ค.</h2>
      <LoginButton />
    </div>
  );
}
// LoginButton.jsx

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

export default function LoginButton(props) {
  const navigate = useNavigate();
  console.log(props);

  function login() {
    setTimeout(() => {
      // ํŽ˜์ด์ง€๋ฅผ ์ด๋™
      navigate("/");
    }, 1000);
  }

  return <button onClick={login}>๋กœ๊ทธ์ธํ•˜๊ธฐ</button>;
}

 

 

 ๐Ÿ”— Redirect  => v6์—์„œ Redirect ์‚ฌ๋ผ์ง !!!!!!

 

import { BrowserRouter, Route, Routes, Link, Navigate } from "react-router-dom";
// import Links from "./components/Links";
import NavLinks from "./components/NavLinks";
import About from "./pages/About";
import Home from "./pages/Home";
import NotFound from "./pages/NotFound";
import Profile from "./pages/Profile";
import Login from "./pages/Login";

const isLogin = true;

function App() {
  return (
    <BrowserRouter>
      ...
      <Routes>
        <Route
          path="/login"
          element={isLogin ? <Navigate replace to="/" /> : <Login />}
        />
       ...
      </Routes>
    </BrowserRouter>
  );
}

export default App;

 

 


 

 

 ๋ฆฌ์•กํŠธ ๊ฐ•์˜๊ฐ€ v5 ๊ธฐ์ค€์ด์–ด์„œ v6 ์ฝ”๋“œ๋กœ ๋ฐ”๊พธ๊ณ  ๊ณ„์† ์ž๋ฃŒ ์ฐพ๋Š๋ผ ์ƒ๊ฐ ํ–ˆ๋˜ ๊ฑฐ ๋ณด๋‹ค ๊ณต๋ถ€ ์‹œ๊ฐ„์ด ๋” ๊ฑธ๋ ธ๋‹ค ํ‘ํ‘ ๊ธฐ๋ณธ ๋‚ด์šฉ์ด๋ผ ๊ทธ๋Ÿฐ์ง€ ๊ฑฐ์˜ ๋ชจ๋“  ๊ฒƒ์ด ๋ฐ”๋€Œ์—ˆ๋‹ค....ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ v6 ๊ฐ•์˜ ์˜€์œผ๋ฉด ์ข‹์•˜์„ํ…๋ฐ ์•„์‰ฝใ… ใ…   ๋„ˆ๋ฌด ์กธ๋ฆฌ๋‹ค ๋‚ด์ผ ํ•™๊ต ๊ฐ€์•ผ ํ•˜๋Š”๋ฐ๐Ÿ‘ฏ‍โ™€๏ธ๐Ÿ‘ฏ‍โ™€๏ธ ๋ฆฌ์•กํŠธ๋Š” ์—„์ฒญ ๋นจ๋ฆฌ ๋ฐ”๋€๋‹ค๊ณ  ํ•˜๋Š”๋ฐ ์ •๋ง ๋งž๋Š” ๊ฑฐ ๊ฐ™๋‹ค ๊นŒํƒˆ๋ ˆ๋‚˜~~~~~~~๐Ÿ’ƒ ๋นจ๋ฆฌ ์ž์•ผ์ง€ ์ง€๊ธˆ ์•ฝ๊ฐ„ ์ •์‹ ์ด ๋‚˜๊ฐ”๊ณ  ๋ˆˆ์ด ๋ฐ˜๋‹ฌ์ด ๋๋‹ค ์•„๋ฌด ๋ง์ด๋‚˜ ๋ง‰ ์“ฐ๊ณ  ์žˆ๋Š” ๊ฑฐ ๊ฐ™๋‹ค ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ใ…ใ…ใ…ใ… ์•ˆ๋…• ๋‚ด์ผ๋„ ํ–‰๋ณตํ•œ ํ•˜๋ฃจ ์•„์ž์•„์ž.....

 

'๐Ÿ’ฌ > ใ…ใ……ใ…Œใ…‹ใ…ใ…… ์ฑŒ๋ฆฐ์ง€' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

37์ผ์ฐจ  (0) 2022.03.23
36์ผ์ฐจ  (0) 2022.03.21
34์ผ์ฐจ  (0) 2022.03.20
33์ผ์ฐจ  (0) 2022.03.19
32์ผ์ฐจ  (0) 2022.03.18