admin管理员组文章数量:1410717
I am using use location to hide the navbar if the pathname name is /admin
, /employee
, /publisher
. but I got an error saying that Error: useLocation() may be used only in the context of a <Router> ponent.
This is My App.js
import { BrowserRouter as Router, Routes, Route, useLocation } from 'react-router-dom';
import Navbar from './ponents/Navbar';
function App() {
const location = useLocation()
return (
<>
<UserState>
<Router>
{!["/admin", "/employee", "/publisher"].includes(location.pathname) && <Navbar/>} //For to hide the navbar if the pathname is /admin, /employee, /publisher
<Routes onUpdate={() => window.scrollTo(0, 0)}>
<Route exact path="/" element={<Home />} />
<Route exact path="/service" element={<Service />} />
<Route exact path="/contact" element={<Contact />} />
<Route exact path="/login" element={<Login />} />
<Route exact path="/reset" element={<Reset />} />
<Route exact path="/reset/:token" element={<Newpassword />} />
<Route path="*" element={<NoteFound/>} />
{/* admin pages */}
<Route path="/admin" element={<Admin />}>
<Route path="add-project" element={<Addproject />} />
<Route path="all-user" element={<AllUser />} />
</Route>
{/* Publisher dasboard */}
<Route path="/publisher" element={<Publisher />}>
<Route path="project-status" element={<ProjectStatus />} />
<Route path="allpublise" element={<Allpublise />} />
</Route>
</Route>
</Routes>
</Router>
</UserState>
</>
);
}
export default App;
I am using use location to hide the navbar if the pathname name is /admin
, /employee
, /publisher
. but I got an error saying that Error: useLocation() may be used only in the context of a <Router> ponent.
This is My App.js
import { BrowserRouter as Router, Routes, Route, useLocation } from 'react-router-dom';
import Navbar from './ponents/Navbar';
function App() {
const location = useLocation()
return (
<>
<UserState>
<Router>
{!["/admin", "/employee", "/publisher"].includes(location.pathname) && <Navbar/>} //For to hide the navbar if the pathname is /admin, /employee, /publisher
<Routes onUpdate={() => window.scrollTo(0, 0)}>
<Route exact path="/" element={<Home />} />
<Route exact path="/service" element={<Service />} />
<Route exact path="/contact" element={<Contact />} />
<Route exact path="/login" element={<Login />} />
<Route exact path="/reset" element={<Reset />} />
<Route exact path="/reset/:token" element={<Newpassword />} />
<Route path="*" element={<NoteFound/>} />
{/* admin pages */}
<Route path="/admin" element={<Admin />}>
<Route path="add-project" element={<Addproject />} />
<Route path="all-user" element={<AllUser />} />
</Route>
{/* Publisher dasboard */}
<Route path="/publisher" element={<Publisher />}>
<Route path="project-status" element={<ProjectStatus />} />
<Route path="allpublise" element={<Allpublise />} />
</Route>
</Route>
</Routes>
</Router>
</UserState>
</>
);
}
export default App;
Share
Improve this question
edited Apr 23, 2022 at 16:40
Drew Reese
204k18 gold badges245 silver badges273 bronze badges
asked Apr 23, 2022 at 12:52
TheDeveloperGuyTheDeveloperGuy
1512 gold badges5 silver badges15 bronze badges
2 Answers
Reset to default 3The useLocation
hook (and all other react-router-dom
hooks) need a router above it in the ReactTree so that a routing context is available.
2 options:
Move the
Router
ponent up the tree and wrap theApp
ponent so it can use theuseLocation
hook.import { BrowserRouter as Router } from 'react-router-dom'; ... <Router> <App /> </Router>
...
import { Routes, Route, useLocation } from 'react-router-dom'; import Navbar from './ponents/Navbar'; function App() { const location = useLocation(); return ( <UserState> {!["/admin", "/employee", "/publisher"].includes(location.pathname) && <Navbar/>} <Routes onUpdate={() => window.scrollTo(0, 0)}> <Route path="/" element={<Home />} /> <Route path="/service" element={<Service />} /> <Route path="/contact" element={<Contact />} /> <Route path="/login" element={<Login />} /> <Route path="/reset" element={<Reset />} /> <Route path="/reset/:token" element={<Newpassword />} /> <Route path="*" element={<NoteFound/>} /> {/* admin pages */} <Route path="/admin" element={<Admin />}> <Route path="add-project" element={<Addproject />} /> <Route path="all-user" element={<AllUser />} /> </Route> {/* Publisher dasboard */} <Route path="/publisher" element={<Publisher />}> <Route path="project-status" element={<ProjectStatus />} /> <Route path="allpublise" element={<Allpublise />} /> </Route> </Routes> </UserState> ); } export default App;
Move the
useLocation
hook down the tree so that it's used within theRouter
ponent.import { Routes, Route, useLocation } from 'react-router-dom'; import Navbar from './ponents/Navbar'; function App() { return ( <UserState> <Router> <Navbar/> <Routes onUpdate={() => window.scrollTo(0, 0)}> <Route path="/" element={<Home />} /> <Route path="/service" element={<Service />} /> <Route path="/contact" element={<Contact />} /> <Route path="/login" element={<Login />} /> <Route path="/reset" element={<Reset />} /> <Route path="/reset/:token" element={<Newpassword />} /> <Route path="*" element={<NoteFound/>} /> {/* admin pages */} <Route path="/admin" element={<Admin />}> <Route path="add-project" element={<Addproject />} /> <Route path="all-user" element={<AllUser />} /> </Route> {/* Publisher dasboard */} <Route path="/publisher" element={<Publisher />}> <Route path="project-status" element={<ProjectStatus />} /> <Route path="allpublise" element={<Allpublise />} /> </Route> </Routes> </Router> </UserState> ); } export default App;
...
const Navbar = () => { const location = useLocation(); return !["/admin", "/employee", "/publisher"].includes(location.pathname) ? /* Return navbar JSX here */ : null; };
useLocation()
hook is used to extract the current location from the router.
For this purpose it's need the router context to pass the Location content
So if you want to use this hook, you need to use it in one of the nested ponents in <Router>
Provider.
For your situation, you need to move the hook into the navbar ponent.
import { useLocation } from 'react-router-dom';
function Navbar() {
const location = useLocation()
if(["/admin", "/employee", "/publisher"].includes(location.pathname))
return <></>
return (
<>
I'm a navbar
</>
);
}
export default Navbar;
本文标签:
版权声明:本文标题:javascript - index.tsx:24 Uncaught Error: useLocation() may be used only in the context of a <Router> component - 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745021444a2638153.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论