简体   繁体   English

反应路由器错误“useRoutes()只能用于<router>成分。”</router>

[英]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:( ) The error said this:所以,当我在控制台中看到这些大红色错误时,我正在制作这个网站(我知道这是一场噩梦:()错误是这样说的:

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

And I didn't even use a "useRoutes" hook.而且我什至没有使用“useRoutes”钩子。 Anyways here's my code:无论如何,这是我的代码:

import Links from "./components/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;

The Routes component uses the useRoutes . Routes组件使用useRoutes Routes needs to be rendered within a routing context provided by a router component. Routes需要在路由器组件提供的路由上下文中呈现。

Additionally the Addition ( sorry, no pun intended ) needs to be passed to the element prop as JSX .此外, Addition抱歉,没有双关语)需要作为 JSX传递给element属性。 This was one of the breaking changes in the Route component API from v5 to v6.这是Route组件 API 从 v5 到 v6 的重大更改之一。

Example:例子:

import Links from "./components/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 component
      <div>
        <h1>Chose your operation:</h1>
        <div className="cards">
          <Routes>
            <Route
              path="/addition"
              element={<Addition />} // <-- passed as JSX
            />
          </Routes>
        </div>
      </div>
    </Router>
  )
}

编辑 react-router-error-useroutes-may-be-used-only-in-the-contect-of-a-router-co

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React js:错误:useLocation()只能在a的上下文中使用<router>成分</router> - React js: Error: useLocation() may be used only in the context of a <Router> component (反应路由器 v6)“错误:useNavigate() 只能在<router>特定组件中的组件”错误</router> - (react router v6) "Error: useNavigate() may be used only in the context of a <Router> component" error in hoc component 未捕获的错误:useLocation() 只能在 a 的上下文中使用<router>零件</router> - Uncaught Error: useLocation() may be used only in the context of a <Router> component npm 测试:useNavigate() 只能在<router>成分</router> - npm test: useNavigate() may be used only in the context of a <Router> component 我如何解析“ useHref() 只能在上下文中使用<router>组件”当组件已经在里面时<router> ?</router></router> - How can I resolver " useHref() may be used only in the context of a <Router> component" when component is already inside <Router>? 试图添加一个详细的产品组件。 React-router - Trying to add a detail product component. React-router 错误:useNavigate() 只能在 a 的上下文中使用<router>组件虽然我的登录页面的路由位于里面<browserrouter></browserrouter></router> - error: useNavigate() may be used only in the context of a <Router> component Although my login page's route is located inside <BrowserRouter> 反应 - 反应路由器 - A<router> 可能只有一个子元素</router> - React - React Router - A <Router> may have only one child element 我不断收到“useNavigate() 只能在<router> component" 运行时出错,我不确定为什么</router> - I keep getting "useNavigate() may be used only in the context of a <Router> component" error when running this and I'm not sure why 反应路由器 | 仅在页面刷新时加载组件 - React Router | load component only on page refresh
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM