useLocation in v4
I'm using
but got Uncaught Error: useLocation() may be used only in the context of a <Router> component.
32 Replies
Hi @ganuzaz. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
It seems like you're trying to use the
useLocation
hook outside of a router context. To fix this issue, make sure you're using the useLocation
hook within a component that is wrapped by a router provider.
In refine, you can use the useRouterContext
hook to access the router context. Here's an example of how to use it:
Also, ensure that you have properly set up the router provider in your App.tsx
or similar file. For example, if you're using React Router v6:
For more information on setting up router providers in refine, refer to the router provider documentation (https://refine.dev/docs/api-reference/core/providers/router-provider).
----------------------
Note: you can ask me a follow up question by @ mentioning me again
:speech_balloon:
----------------------<@149140198213550080> kapa.ai is still learning and improving, please let us how it did by reacting below
fascinating-indigo•16mo ago
Hi, bot is right. are u using useLocation inside <BrowserRouter> ?
optimistic-gold•16mo ago
yes
i think its just the wrong import
im trying it rn
fascinating-indigo•16mo ago
import { useLocation } from 'react-router-dom';
is this import cause error ?
optimistic-gold•16mo ago
yes it was that
then i changed it into
import { useRouterContext } from "@refinedev/core";
const {useLocation} = useRouterContext()
const location = useLocation()
still same error
my App.tsx is basically like this
I might miss something tho because im migrating from v3
fascinating-indigo•16mo ago
In which component are you using useLocation?
optimistic-gold•16mo ago
create component
const ConfigurationCreate: React.FC = () => {}
fascinating-indigo•16mo ago
is ConfigurationCreate inside resource ?
or, as a child of <BrowserRouter> ?
optimistic-gold•16mo ago
inside the ...configuration
fascinating-indigo•16mo ago
oh ok
optimistic-gold•16mo ago
configuration is is an array of resources
{
name: "config/area",
meta: {
label: "Configuration,
route: "config/area",
parent : "config",
icon: <IoTimerOutline/>,
},
create: ConfigurationCreate
},
fascinating-indigo•16mo ago
i try to reproduce the problem but couldn't yet
optimistic-gold•16mo ago
kind of like that
hmm
and the parent is like this
even if i removed the resources props in the Refine element, the error still popping out
could there be something im missing from package.json or vite stuffs?
fascinating-indigo•16mo ago
maybe. it's a long shot but you can remove package.lock.json and node_modules and try to install again