useLocation
is not available on gatsby
#36233
Replies: 2 comments
-
Yes, this makes sense. We will accept a change that will introduce this re-export if someone is willing to implement it. To implement the it, export will need to be added in https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/cache-dir/gatsby-browser-entry.js |
Beta Was this translation helpful? Give feedback.
-
Big upvote from us for this. A gross workaround is to implement your own TypeScriptimport type { FC, ReactNode } from "react";
import { createContext, useContext } from "react";
const LocationContext = createContext<Location | null>(null);
interface LocationProviderProps {
location: Location;
children: ReactNode;
}
export const LocationProvider: FC<LocationProviderProps> = ({
location,
children,
}) => (
<LocationContext.Provider value={location}>
{children}
</LocationContext.Provider>
);
export const useLocation = (): Location => {
const location = useContext(LocationContext);
if (location == null) {
throw new Error("useLocation must be used inside LocationProvider");
}
return location;
}; JavaScriptimport { createContext, useContext } from "react";
const LocationContext = createContext(null);
export const LocationProvider = ({ location, children }) => (
<LocationContext.Provider value={location}>
{children}
</LocationContext.Provider>
);
export const useLocation = () => {
const location = useContext(LocationContext);
if (location == null) {
throw new Error("useLocation must be used inside LocationProvider");
}
return location;
}; Now just wrap any page with |
Beta Was this translation helpful? Give feedback.
-
If I want to use Reach Routers
useLocation
I have to:import { useLocation } from '@gatsbyjs/reach-router';
as opposed to
navigate
,useLocation
is not available ongatsby
itself.I don't really like it, that I have to install
@gatsbyjs/reach-router
myself.Could we export
useLocation
ingatsby
?(by the way, came here, because I realized there's a difference in
navigate
imported from@gatsbyjs/reach-router
andgatsby
. Both work, but only the one fromgatsby
makes the page load correctly in develop)Beta Was this translation helpful? Give feedback.
All reactions