๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“‚ ์‚ฝ์งˆ ๋…ธํŠธ/์—๋Ÿฌ ํ•ด๊ฒฐ

Route, Routes๋ฅผ BrowserRouter๋กœ ๊ฐ์‹ธ์ฃผ์ง€ ์•Š์œผ๋ฉด..

by Dev. Ella 2023. 5. 19.

๐Ÿค”  ์˜ค๋Š˜์˜ ์—๋Ÿฌ

React๋กœ ์ฝ”๋“œ๋ฅผ ์งœ๋˜ ์ค‘, ์˜ค๋Š˜์€ ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ๋ฅผ ๋งŒ๋‚ฌ๋‹ค.

์–ธ์ œ ๋ด๋„ ๋ฌด์„œ์šด ๋ฆฌ์•กํŠธ์˜ ์—๋Ÿฌ ํ™”๋ฉด (...)

TypeError: Cannot read properties of undefined (reading 'pathname')

 

ํ•ด๋‹น ์—๋Ÿฌ์— ๋Œ€ํ•œ GPT์˜ ๋‹ต์€ ์•„๋ž˜์™€ ๊ฐ™์•˜๋‹ค.

์ด ์—๋Ÿฌ๋Š” React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ 'pathname' ์†์„ฑ์„ ๊ฐ–๋Š” ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋ ค๊ณ  ํ•  ๋•Œ ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ์ •์˜๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. 'pathname'์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ธŒ๋ผ์šฐ์ €์˜ URL ๊ฒฝ๋กœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์†์„ฑ์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ด ์—๋Ÿฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒํ™ฉ์—์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

1. ๋ธŒ๋ผ์šฐ์ € ๋ผ์šฐํ„ฐ ์˜ค๋ฅ˜: React Router ๋˜๋Š” ๋‹ค๋ฅธ ๋ผ์šฐํŒ… ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ๋ธŒ๋ผ์šฐ์ € ๋ผ์šฐํ„ฐ๊ฐ€ ์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์•˜๊ฑฐ๋‚˜ ์ž˜๋ชป ๊ตฌ์„ฑ๋˜์–ด 'pathname' ์†์„ฑ์ด ์ •์˜๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ๋ผ์šฐํ„ฐ ์„ค์ •์„ ํ™•์ธํ•˜๊ณ  ์ดˆ๊ธฐํ™”๋˜์—ˆ๋Š”์ง€, ์ •ํ™•ํ•œ ๊ฒฝ๋กœ ๋ฐ ๋ผ์šฐํŠธ ๊ตฌ์„ฑ์„ ์‚ฌ์šฉํ•˜๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

2. ์ปดํฌ๋„ŒํŠธ ์ƒ๋ช…์ฃผ๊ธฐ ๋ฌธ์ œ: React ์ปดํฌ๋„ŒํŠธ์˜ ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ๋‚˜ ํŠน์ • ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์—์„œ 'pathname' ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์„ ๋•Œ, ํ•ด๋‹น ์†์„ฑ์ด ์•„์ง ์ •์˜๋˜์ง€ ์•Š์•˜์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งˆ์šดํŠธ ๋˜๊ธฐ ์ „์— ๋˜๋Š” ํ•ด๋‹น ์†์„ฑ์ด ์˜ˆ์ƒ๋Œ€๋กœ ์„ค์ •๋˜์ง€ ์•Š์•˜์„ ๋•Œ ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

3. ๋น„๋™๊ธฐ ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ ๋ฌธ์ œ: ๋น„๋™๊ธฐ ์ž‘์—…(์˜ˆ: ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ)์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋™์•ˆ 'pathname' ์†์„ฑ์„ ์ฐธ์กฐํ•˜๊ณ ์ž ํ•  ๋•Œ, ํ•ด๋‹น ์ž‘์—…์ด ์•„์ง ์™„๋ฃŒ๋˜์ง€ ์•Š์•„ 'pathname'์ด ์ •์˜๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ์ด ์™„๋ฃŒ๋œ ํ›„์— ํ•ด๋‹น ์†์„ฑ์„ ์ฐธ์กฐํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

 

๐Ÿง ์ฒ˜์Œ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

์ฒ˜์Œ์—๋Š” Routes์™€ Route๋“ค์„ 'Router'๋กœ ๋ฌถ์–ด ์ฃผ์—ˆ๋‹ค. ์ด ๋ถ€๋ถ„์ด ์›์ธ์ด์—ˆ๋‹ค. 

// App.js

import './App.css';
import { Route, Router, Routes } from "react-router-dom"; // *์—ฌ๊ธฐ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•จ

import BookmarkPage from "../src/pages/Bookmark-page";
import Footer from "../src/components/Footer";
import Header from "../src/components/Header";
import MainPage from "../src/pages/Main-page";
import ProductListPage from "../src/pages/ProductList-page";
import React from "react";

function App() {
  return (
    <Router> {/* ์—ฌ๊ธฐ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•จ */}
    <div>
      <Header/>
        <Routes>
          <Route path="/" element={<MainPage/>}/>
          <Route path="/products" element={<BookmarkPage/>}/>
          <Route path="/bookmark" element={<ProductListPage/>}/>
        </Routes>
      <Footer/>
    </div>
    </Router>
  );
}

export default App;

 

 

๐Ÿ’ก ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

1. App.js์— ๋‹ค ๋•Œ๋ ค๋ฐ•๊ธฐ

App.js ํŒŒ์ผ์— BrowserRouter, Route, Routes๋ฅผ ๋ชจ๋‘ import ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Route๋ฅผ Routes๋กœ ๋ฌถ์–ด์ฃผ๊ณ , ์ด ๋‘˜์„ ๋ชจ๋‘ BrowserRouter๋กœ ๋ฌถ์–ด์ค€๋‹ค.

// App.js

import './App.css';
import { BrowserRouter, Route, Routes } from "react-router-dom"; // *BrowserRouter๋ฅผ import

import BookmarkPage from "../src/pages/Bookmark-page";
import Footer from "../src/components/Footer";
import Header from "../src/components/Header";
import MainPage from "../src/pages/Main-page";
import ProductListPage from "../src/pages/ProductList-page";
import React from "react";

function App() {
  return (
    <BrowserRouter>
    <div>
      <Header/>
        <Routes>
          <Route path="/" element={<MainPage/>}/>
          <Route path="/products" element={<BookmarkPage/>}/>
          <Route path="/bookmark" element={<ProductListPage/>}/>
        </Routes>
      <Footer/>
    </div>
    </BrowserRouter>
  );
}

export default App;

 

2. index.js์— App.js๋ฅผ import ํ•˜๊ธฐ

๋˜๋Š” App.js์—๋Š” Route์™€ Routes๋งŒ import ํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค. ์ด๋•Œ, BrowserRouter๋Š” index.js์— import ๋ฐ jsx์—์„œ ์‚ฌ์šฉ, App ์ปดํฌ๋„ŒํŠธ ๋˜ํ•œ index.js์— import ๋ฐ jsx์—์„œ ์‚ฌ์šฉํ•ด ์ค€๋‹ค.

// App.js

import './App.css';
import { Route, Routes } from "react-router-dom"; // *Route์™€ Routes๋งŒ import

import BookmarkPage from "../src/pages/Bookmark-page";
import Footer from "../src/components/Footer";
import Header from "../src/components/Header";
import MainPage from "../src/pages/Main-page";
import ProductListPage from "../src/pages/ProductList-page";
import React from "react";

// ์—ฌ๊ธฐ์„œ๋Š” ๋”ฑํžˆ BrowserRouter๋ฅผ ์“ฐ์ง€ ์•Š์Œ
function App() {
  return (
    <div>
      <Header/>
        <Routes>
          <Route path="/" element={<MainPage/>}/>
          <Route path="/products" element={<BookmarkPage/>}/>
          <Route path="/bookmark" element={<ProductListPage/>}/>
        </Routes>
      <Footer/>
    </div>
  );
}

export default App;
// index.js

import './index.css';
import App from './App'; // *App.js๋ฅผ import
import { BrowserRouter } from 'react-router-dom'; // *BrowserRouter import
import React from 'react';
import ReactDOM from 'react-dom/client';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
      <BrowserRouter>
        <App /> {/* importํ•œ App ์ปดํฌ๋„ŒํŠธ๋ฅผ jsx์—์„œ ์‚ฌ์šฉ */}
      </BrowserRouter>
  </React.StrictMode>
);

 

 

๐Ÿ˜… Today I learned

ํ•˜์•„, ๋ถ„๋ช… ๋ผ์šฐํ„ฐ ๊ฐœ๋…์— ๋Œ€ํ•ด ์ˆ™์ง€ํ–ˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ์•„๋‹ˆ์—ˆ๋‹ค. ๋‹น์—ฐํžˆ ์•Œ๊ณ  ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•œ ๋ถ€๋ถ„๋„ ํ‹€๋ฆฌ๊ธฐ ๋งˆ๋ จ์ด๋‹ค. ์ด ์—๋Ÿฌ ํ•ด๊ฒฐ ์‚ฝ์งˆ ๋…ธํŠธ๋Š” ์˜ค๋‹ต ๋…ธํŠธ๋ผ ์ƒ๊ฐํ•˜๊ณ , ์•ž์œผ๋กœ๋Š” ๊ฐ™์€ ์‹ค์ˆ˜๋ฅผ ํ•˜์ง€ ์•Š๊ฒ ๋‹ค๋Š” ๋‹ค์ง์„ ํ•œ๋‹ค. 

 

 

๋Œ“๊ธ€