简体   繁体   English

未捕获的错误:useLocation() 只能在 a 的上下文中使用<router>零件</router>

[英]Uncaught Error: useLocation() may be used only in the context of a <Router> component

I'm getting this error while using useLocation in Navbar component:在导航Navbar组件中使用useLocation时出现此错误:

Uncaught Error: useLocation() may be used only in the context of a <Router> component未捕获的错误: useLocation()只能在<Router>组件的上下文中使用

import React, { useEffect, } from 'react'
import { Link, useLocation } from 'react-router-dom'

const Navbar = () => {
  let location = useLocation();

  useEffect(() => {
    console.log(location);
  }, [location]);

  return (
    <div>
      <nav className="navbar navbar-expand-lg navbar-dark bg-dark">
        <div className="container-fluid">
          <Link className="navbar-brand" to="/navbar">Navbar</Link>
          <button className="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span className="navbar-toggler-icon"></span>
          </button>
          <div className="collapse navbar-collapse" id="navbarSupportedContent">
            <ul className="navbar-nav me-auto mb-2 mb-lg-0">
              <li className="nav-item">
                <Link className="nav-link active" aria-current="page" to="/">Home</Link>
              </li>
              <li className="nav-item">
                <Link className="nav-link" to="/about">About</Link>
              </li>
            </ul>
            <form className="d-flex" role="search">
              <input className="form-control me-2" type="search" placeholder="Search" aria-label="Search" />
              <button className="btn btn-outline-success" type="submit">Search</button>
            </form>
          </div>
        </div>
      </nav>
    </div>
  )
}

export default Navbar

The error is informing you that the Navbar component necessarily needs to be rendered within the routing context provided by one of the routers exported by react-router / react-router-dom .该错误通知您react-router组件必须在由Navbar / react-router-dom导出的路由器之一提供的路由上下文中呈现。

Example:例子:

import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import Navbar from '../path/to/Navbar';

const App = () => (
  <Router> // <-- router provides routing context to descendents
    <Navbar /> // <-- rendered within a router component
    <Routes>
      <Route path="/" element={<h1>Home</h1>} />
      ... other routes ...
    </Routes>
  </Router>
);

暂无
暂无

声明:本站的技术帖子网页,遵循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 我如何解析“ 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>? 错误: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> useLocation() 即使在路由器上下文中也不起作用 - useLocation() is not working even inside the Router context 未捕获的错误:未提供上下文:useLeafletContext() 只能用于<mapcontainer></mapcontainer> - Uncaught Error: No context provided: useLeafletContext() can only be used in a descendant of <MapContainer> 错误是:A<Router> 可能只有一个子元素 - Error is: A <Router> may have only one child element 未捕获的TypeError:this.context.router.push不是函数 - Uncaught TypeError: this.context.router.push is not a function 未捕获的错误: <Link> 在路由器上下文之外呈现的不能导航。(…)反应 - Uncaught Error: <Link>s rendered outside of a router context cannot navigate.(…) react 未捕获错误:不允许。 不受信任的代码只能按ID更新文档。 [403] - Uncaught Error: Not permitted. Untrusted code may only update documents by ID. [403]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM