๐ฆฆ
35์ผ์ฐจ
Part 10. React ๋ก ์ผํ๋ชฐ ๋ง๋ค๊ธฐ (React ๊ธฐ๋ณธ)
Ch 4. React Router
Ch 4. React Router
$ 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
https://github.com/sindresorhus/query-string
$ 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 ๊ฐ์ ์์ผ๋ฉด ์ข์์ํ ๋ฐ ์์ฝใ ใ ๋๋ฌด ์กธ๋ฆฌ๋ค ๋ด์ผ ํ๊ต ๊ฐ์ผ ํ๋๋ฐ๐ฏโ๏ธ๐ฏโ๏ธ ๋ฆฌ์กํธ๋ ์์ฒญ ๋นจ๋ฆฌ ๋ฐ๋๋ค๊ณ ํ๋๋ฐ ์ ๋ง ๋ง๋ ๊ฑฐ ๊ฐ๋ค ๊นํ๋ ๋~~~~~~~๐ ๋นจ๋ฆฌ ์์ผ์ง ์ง๊ธ ์ฝ๊ฐ ์ ์ ์ด ๋๊ฐ๊ณ ๋์ด ๋ฐ๋ฌ์ด ๋๋ค ์๋ฌด ๋ง์ด๋ ๋ง ์ฐ๊ณ ์๋ ๊ฑฐ ๊ฐ๋ค ํํํํํใ ใ ใ ใ ์๋ ๋ด์ผ๋ ํ๋ณตํ ํ๋ฃจ ์์์์.....