mirror of
https://github.com/Stone-Red-Code/website.git
synced 2026-09-05 23:43:37 +02:00
refact: extract location to provider/hook
This commit is contained in:
@@ -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>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user