본문 바로가기
공부이야기/[React]

[React 오류] A <Route> is only ever to be used as the child of <Routes> element, never rendered directly. Please wrap your <Route> in a <Routes>.

by Nameless 2022. 1. 2.

react-router-dom 버전이 5->6으로 업데이트되었다고 한다.

내가 공부하고 있는 버전은 5라서 계속 오류가 떴나보다ㅠ

 

※ 참고 사이트

https://stackoverflow.com/questions/69832748/error-a-route-is-only-ever-to-be-used-as-the-child-of-routes-element

 

Error: A <Route> is only ever to be used as the child of <Routes> element

Trying to use routing for the first time, and followed the exact instructions from Udemy: App.js: import { Route } from "react-router-dom"; import Welcome from "./Pages/Welcome";

stackoverflow.com


★ 결론

  • Route구성 요소를 Routes로 래핑
  • component를 element로 바꾼다
  • JSX 문법 사용

 

이전버전

import {BrowserRouter as Router, Route} from "react-router-dom";
...
function App(){
  return(
    <ThemeProvider theme={theme}>
        <GlobalStyle/>
        <Router>
          <Route path="/quiz" component={Quiz}/>
        </Router>
    </ThemeProvider>
  );
}
...

 

업데이트 버전

import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
...
const App = () => {
    return (
        <ThemeProvider theme={theme}>
            <GlobalStyle />
            <Router>
                <Routes>
                  <Route path="/quiz" element={<Quiz/>} />
                </Routes>
            </Router>
        </ThemeProvider>
    );
};
...

'공부이야기 > [React]' 카테고리의 다른 글

React-Quiz 만들기 [6] : import export 이용  (0) 2021.12.31
React-Quiz 만들기 [5]  (0) 2021.12.31
React-Quiz 만들기 [4]  (0) 2021.12.26
React-Quiz 만들기 [3]  (0) 2021.12.26
React-Quiz 만들기 [2]  (0) 2021.12.24

댓글