Skip to content

Migrate to new Data API #10236

Closed Answered by brophdawg11
Sagie501 asked this question in Q&A
Discussion options

You must be logged in to vote

This is because you're using useLocation outside of RouterProvider. Instead, you can move anything global like the outer logic in your App component to a root layout component so it's inside the router:

const router = createHashRouter(
    createRoutesFromElements(
        <Route element={<Layout />}>
            <Route path="/" element={<Home />} />
            {/* More Route components here */}
        </Route>,
    ),
);

const Layout: FC = () => { 
    const location = useLocation();

    useEffect(() => {
        // Doing something with the location.pathname here
    }, [location.pathname]);
    
    return <Outlet />
}

export const App: FC = () => {
    return <RouterProvider router={

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Sagie501
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants