refact: extract location to provider/hook

This commit is contained in:
Jean-Philippe Sirois
2019-11-08 17:04:09 -08:00
parent ab43fee166
commit 3b3a68698f
4 changed files with 60 additions and 8 deletions
+33
View File
@@ -0,0 +1,33 @@
import { WindowLocation } from "@reach/router"
import React, { FC, PropsWithChildren, useMemo } from "react"
export interface ILocationContextInterface {
location: WindowLocation | void
isHome: boolean
}
interface ILocationProviderProps {
location: WindowLocation | void
}
export const LocationContext = React.createContext<ILocationContextInterface | null>(
null
)
export const LocationProvider: FC<
PropsWithChildren<ILocationProviderProps>
> = ({ children, location }) => {
const memoizedContextValue = useMemo(
() => ({
location,
isHome: location ? location.pathname === "/" : false,
}),
[location]
)
return (
<LocationContext.Provider value={memoizedContextValue}>
{children}
</LocationContext.Provider>
)
}