javascript - React router error "useRoutes() may be used only in the contect of a <Router> component.&

So, I was making this website when I saw these BIG red errors in the console ( I know a nightmare :( )

So, I was making this website when I saw these BIG red errors in the console ( I know a nightmare :( ) The error said this:

Uncaught Error: useRoutes() may be used only in the context of a <Router> ponent.

And I didn't even use a "useRoutes" hook. Anyways here's my code:

import Links from "./ponents/Links"
import Addition from "./pages/Addition"
import "./styles/App.css"
import { BrowserRouter as Router, Switch, Route, Routes } from 'react-router-dom';

function App() {
  return (
    <div>
      <h1>Chose your operation:</h1>
      <div className="cards">
        <Routes>
          <Route path="/addition" element={Addition} />
        </Routes>
      </div>
    </div>
  )
}

export default App;

So, I was making this website when I saw these BIG red errors in the console ( I know a nightmare :( ) The error said this:

Uncaught Error: useRoutes() may be used only in the context of a <Router> ponent.

And I didn't even use a "useRoutes" hook. Anyways here's my code:

import Links from "./ponents/Links"
import Addition from "./pages/Addition"
import "./styles/App.css"
import { BrowserRouter as Router, Switch, Route, Routes } from 'react-router-dom';

function App() {
  return (
    <div>
      <h1>Chose your operation:</h1>
      <div className="cards">
        <Routes>
          <Route path="/addition" element={Addition} />
        </Routes>
      </div>
    </div>
  )
}

export default App;
Share Improve this question asked Oct 3, 2022 at 23:40 Arjun PArjun P 391 silver badge5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

The Routes ponent uses the useRoutes. Routes needs to be rendered within a routing context provided by a router ponent.

Additionally the Addition (sorry, no pun intended) needs to be passed to the element prop as JSX. This was one of the breaking changes in the Route ponent API from v5 to v6.

Example:

import Links from "./ponents/Links"
import Addition from "./pages/Addition"
import "./styles/App.css"
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';

function App() {
  return (
    <Router> // <-- render app into Router ponent
      <div>
        <h1>Chose your operation:</h1>
        <div className="cards">
          <Routes>
            <Route
              path="/addition"
              element={<Addition />} // <-- passed as JSX
            />
          </Routes>
        </div>
      </div>
    </Router>
  )
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745486451a4629789.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信