๐Ÿšซ ๋กœ๊ทธ์ธ ์ƒํƒœ์— ๋”ฐ๋ฅธ ์ ‘๊ทผ ์ œํ•œ ๊ตฌํ˜„ (+ ๋นˆ ํŽ˜์ด์ง€ ์„ค์ •)

Doozuuยท2023๋…„ 8์›” 2์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
22/23

๋กœ๊ทธ์ธ ์ƒํƒœ์— ๋”ฐ๋ผ ์ ‘๊ทผ ์ œํ•œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ–ˆ๋‹ค.

X ๋กœ๊ทธ์ธ ๋˜์ง€ ์•Š์€ ์ƒํƒœ์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ํŽ˜์ด์ง€

: ์‹œ์ž‘ ํŽ˜์ด์ง€, ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€, ๋กœ๋”ฉ ํŽ˜์ด์ง€, ๋นˆ ํŽ˜์ด์ง€

O ๋กœ๊ทธ์ธ ๋œ ์ƒํƒœ์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ํŽ˜์ด์ง€

: ๋‚˜๋จธ์ง€ ๋ชจ๋“  ํŽ˜์ด์ง€

+ ์ง€์ •๋˜์ง€ ์•Š์€ route์— ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋”ฐ๋กœ ๋””์ž์ธํ•œ empty page๊ฐ€ ๋œจ๋„๋ก ์„ค์ •



1. ๋กœ๊ทธ์ธ ์ƒํƒœ ํ™•์ธ

๋กœ๊ทธ์ธ ์‹œ์— localstorage์— access token์„ ์ €์žฅํ•˜๋„๋ก ์„ค์ •ํ•ด๋‘์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋งŒ์•ฝ localstorage์— ํ† ํฐ์ด ์žˆ๋‹ค๋ฉด ๋กœ๊ทธ์ธ์ด ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์ด๊ณ  ์—†๋‹ค๋ฉด ๋กœ๊ทธ์ธ์ด ๋˜์–ด ์žˆ์ง€ ์•Š์€ ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ํ† ํฐ ์ €์žฅ ์—ฌ๋ถ€๋ฅผ ํ†ตํ•ด ๋กœ๊ทธ์ธ์ด ๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•ด์ค€๋‹ค.

2. ๋กœ๊ทธ์ธ ์ƒํƒœ์— ๋”ฐ๋ฅธ route ์„ค์ •

๋กœ๊ทธ์ธ์ด ๋˜์–ด ์žˆ์„ ๊ฒฝ์šฐ Outlet์„ ์ด์šฉํ•ด ์ง€์ •ํ•œ ๋ผ์šฐํŠธ๋“ค์„ ๋ Œ๋”๋ง ํ•ด์ฃผ๊ณ , ๋กœ๊ทธ์ธ์ด ๋˜์–ด ์žˆ์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋กœ๊ทธ์ธ์„ ํ•˜๋„๋ก ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ์ด๋™ ์‹œํ‚จ๋‹ค.

์ฐธ๊ณ  | Outlet์ด๋ž€?

react-router-dom์˜ Outlet์€ React Router v6์—์„œ ๋„์ž…๋œ ๊ธฐ๋Šฅ์ด๋‹ค. ์ด์ „ ๋ฒ„์ „์˜ React Router์—์„œ๋Š” Route ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ผ์šฐํŒ…์„ ๊ด€๋ฆฌํ–ˆ์ง€๋งŒ, v6์—์„œ๋Š” ๋” ์œ ์—ฐํ•˜๊ณ  ์ค‘์ฒฉ๋œ ๋ผ์šฐํŒ… ๊ตฌ์กฐ๋ฅผ ์œ„ํ•ด Outlet์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ๊ฐœ๋…์ด ๋„์ž…๋˜์—ˆ๋‹ค.
Outlet์€ ๋ถ€๋ชจ ๋ผ์šฐํŠธ์˜ ์ž์‹ ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ๋ Œ๋”๋ง๋˜๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ์ค‘์ฒฉ๋œ ๋ผ์šฐํŒ…์„ ๋‹ค๋ฃจ๋Š” ๋ฐ ์œ ์šฉํ•œ ๋„๊ตฌ์ด๋‹ค. ๋ถ€๋ชจ ๋ผ์šฐํŠธ๊ฐ€ ์ž์‹ ๋ผ์šฐํŠธ๋ฅผ ๊ฐ–๋Š” ๊ฒฝ์šฐ, Outlet์€ ๊ทธ ์ž์‹ ๋ผ์šฐํŠธ๋“ค์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ Œ๋”๋งํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

3. ๋นˆ ํŽ˜์ด์ง€ ์„ค์ •

์–ด๋–ค route์—๋„ ํ•ด๋‹นํ•˜์ง€ ์•Š๋Š” ์ฃผ์†Œ๋กœ ์ด๋™ํ–ˆ์„ ์‹œ์—๋Š” ๋”ฐ๋กœ ๋””์ž์ธํ•œ empty page๋ฅผ ๋ณด์—ฌ์ฃผ๋„๋ก Routes์˜ ๋งˆ์ง€๋ง‰์— <Route path="*" element={<Empty />} /> ๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.


PrivateRoute.js

import { Navigate, Outlet } from "react-router-dom";

const isLogin = !!localStorage.getItem("bagtoken");

const PrivateRoute = () => {
  return isLogin ? <Outlet /> : <Navigate to="/login" />;
};

export default PrivateRoute;

App.js

import { Routes, Route } from "react-router-dom";
import GoogleLogin from "./pages/GoogleLoginPage";
import Nickname from "./pages/NicknamePage";
import ChatListPage from "./pages/ChatListPage";
import ChatRoomPage from "./pages/ChatRoomPage";
import Loading from "./pages/LoadingPage";
import DetailPage from "./pages/DetailPage";
import CreateSalesPage from "./pages/CreateSalesPage";
import ModifySalesPage from "./pages/ModifySalesPage";
import ItemListPage from "./pages/ItemListPage";
import FavoritesPage from "./pages/FavoritesPage";
import MyPage from "./pages/MyPage";
import Start from "./pages/Start";
import Purchase from "./pages/Purchase";
import Empty from "./pages/EmptyPage";

import PrivateRoute from "./components/Route/PrivateRoute";

function App() {
  return (
    <Routes>
      <Route path="/" element={<Start />} />
      <Route path="/login" element={<GoogleLogin />} />
      <Route path="/loading" element={<Loading />} />
      {/* ๋กœ๊ทธ์ธ ํ•ด์•ผ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ํŽ˜์ด์ง€ */}
      <Route element={<PrivateRoute />}>
        <Route path="/nickname" element={<Nickname />} />
        <Route path="/home" element={<ItemListPage />} />
        <Route path="/favorites" element={<FavoritesPage />} />
        <Route path="/chats" element={<ChatListPage />} />
        <Route path="/chats/:roomId" element={<ChatRoomPage />} />
        <Route path="/detail/:postId" element={<DetailPage />} />
        <Route path="/create" element={<CreateSalesPage />} />
        <Route path="/modify/:postId" element={<ModifySalesPage />} />
        <Route path="/mypage" element={<MyPage />} />
        <Route path="/deal" element={<Purchase />} />
        <Route path="/" element={<Start />} />
      </Route>
      {/* ๋นˆ ํŽ˜์ด์ง€ */}
      <Route path="*" element={<Empty />} />
    </Routes>
  );
}

export default App;
profile
๋ชจ๋“ ๊ฒŒ ์ƒˆ๋กญ๊ณ  ์žฌ๋ฐŒ๋Š” ํ”„๋ก ํŠธ์—”๋“œ ์ƒˆ์‹น

0๊ฐœ์˜ ๋Œ“๊ธ€