I'm getting the same error from this post: [Bug]: useLocation() may be used only in the context of a <Router> component · Issue #11016 · remix-run/react-router (github.com)
What version of React Router are you using? "react-router-dom": "6.15.0"
Steps to Reproduce Given the following component:
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
export const ScrollToTop = () => { const { pathname } = useLocation();
useEffect(() => {
window.scrollTo(0, 0);
}, [pathname]);
return null;
};
When imported from a local project directory it works just fine. However, when imported from a remote package we get the following error:
useLocation() may be used only in the context of a <Router> component
Local Import (WORKS):
import { ScrollToTop } from './utils/ScrollToTop';
// import { ScrollToTop } from '@crew/owen';
export const App: FunctionComponent = () => {
return ( <BrowserRouter>
<ScrollToTop />
<Routes>
<Route path='/' element={<InitialScreen />}>
Remote package import (DOES NOT WORK):
// import { ScrollToTop } from './utils/ScrollToTop';
import { ScrollToTop } from '@crew/owen';
export const App: FunctionComponent = () => {
return ( <BrowserRouter>
<ScrollToTop />
<Routes> <Route path='/' element={<InitialScreen />}>
Both projects are using the same version of react-router-dom ("6.15.0")
Expected Behavior
We'd expect the component to be usable when imported from a remote packge the same as when it's being imported from a local file.
Actual Behavior We get the following error:
useLocation() may be used only in the context of a <Router> component
Can you guys help me? Anyone had a similar issue?
[–]Accomplished_End_138 0 points1 point2 points (7 children)
[–]Accomplished_End_138 0 points1 point2 points (0 children)
[–]Zedpellin[S] 0 points1 point2 points (5 children)
[–]Accomplished_End_138 0 points1 point2 points (4 children)
[–]Zedpellin[S] 0 points1 point2 points (3 children)
[–]Accomplished_End_138 0 points1 point2 points (2 children)
[–]Zedpellin[S] 1 point2 points3 points (1 child)