Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

useLocation() may be used only in the context of a <Router> component?

I want to have an animation when a component unmounts.
Here’s the code of the app.js:

import { BrowserRouter, Routes, Route, useLocation } from 'react-router-dom';
import { AnimatePresence } from 'framer-motion';
function App() {
  const location = useLocation();
  return (
    <BrowserRouter>
      <AnimatePresence mode='wait'>
        <Routes key={location.pathname} location={location}>
          <Route path='/' element={<Portfolio />}>
            <Route index path='home' element={<Home />}/>
            <Route path='projects' element={<Projects />}/>
            <Route path='info' element={<Info />}/>
            <Route path='contact' element={<Contact />}/>
          </Route>
        </Routes>
      </AnimatePresence>
    </BrowserRouter>
  );
}
export default App;

And here is the code of the ‘root router’:

function Portfolio() {
    return (
        <Container className='bg' 
        maxWidth = {false}
        <Container className ='cont'
        maxWidth = {false}
        >
            <h1 className='header'>Milos Glamocak</h1>
            <nav className='navContainer'>
                <ul className='menu'>
                    <li className='menuLine'><Link to ='home' className='link' >Home</Link></li>
                    <li className='menuLine'><Link to ='projects' className='link'>Projects</Link></li>
                   <li className='menuLine'><Link to ='info' className='link'>Info</Link></li>
                    <li className='menuLine'><Link to ='contact' className='link'>Contact</Link></li>
                </ul>
                <Outlet />
            </nav>
        </Container>
...
        </Container>
    )
}

I tried looking for solution on the internet but to no avail. Any help appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The <BrowserRouter> needs to be higher up the component tree then where you call useLocation(). Instead, you’re calling useLocation in App, and there is no <BrowserRouter> above App (just one inside of App, which does not help). The solution is to move the browser router up the tree or move useLocation down. For example:

function App() {
  return (
    <BrowserRouter>
      <MyRoutes/>
    </BrowserRouter>
  )
}

function MyRoutes(){
  const location = useLocation();
  return (
    <AnimatePresence mode='wait'>
      <Routes key={location.pathname} location={location}>
        <Route path='/' element={<Portfolio />}>
    // ... etc

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading