[React ์—๋Ÿฌ ์ผ์ง€] ... is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>

nemoยท2022๋…„ 2์›” 7์ผ
4

React

๋ชฉ๋ก ๋ณด๊ธฐ
7/28

๐Ÿšซ [...] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>

react-router-dom v6๋ถ€ํ„ฐ๋Š”,

  • Switch ๋Œ€์‹  Routes๋ฅผ ์‚ฌ์šฉ
  • Route ์•ˆ์— component ๋Œ€์‹  element ์‚ฌ์šฉ

๊ทธ๋ฆฌ๊ณ  <Routes> ์ž์‹์œผ๋กœ๋Š” <Route>๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ v6 ์ด์ „์ฒ˜๋Ÿผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

import {
  BrowserRouter as Router,
  Routes,
  Route,
  Link
} from "react-router-dom";

function App() {
  return (
    <div className="App">
      <Router>
        <Routes>
          <Route path="/">
            <LandingPage />
          </Route>
          <Route path="/login">
            <LoginPage />
          </Route>
          <Route path="/register">
            <RegisterPage />
          </Route>
        </Routes>
      </Router>
    </div>
  );
}

export default App;

<Routes> ์ž์‹์œผ๋กœ๋Š” <Route>๋งŒ ๊ฐ€๋Šฅํ•œ๋ฐ <LandingPage />, <LoginPage />, <RegisterPage />๊ฐ€ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์ด๋‹ค.


์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

import {
  BrowserRouter as Router,
  Routes,
  Route,
  Link
} from "react-router-dom";

function App() {
  return (
    <div className="App">
      <Router>
        <Routes>
          <Route path="/" element={ <LandingPage /> } />
          <Route path="/login" element={ <LoginPage /> } />
          <Route path="/register" element={ <RegisterPage /> } />
        </Routes>
      </Router>
    </div>
  );
}

export default App;

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

comment-user-thumbnail
2023๋…„ 11์›” 9์ผ

์ •๋ง ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!!!!!!!!!!!

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ