import {createContext, PropsWithChildren, useContext, useEffect, useState, } from "react"; import {DEFAULT, Path, Router} from "./router"; const RouterContext = createContext(DEFAULT); export function useRouter() { return useContext(RouterContext); } export function RouterProvider(props : PropsWithChildren) { // path is replaced on every state push/replace/pop const [path, setPath] = useState(window.location.pathname as Path); // handle the back/forward button useEffect(() => { function subscribe(ev: PopStateEvent) { setPath(ev.state as Path); } window.addEventListener("popstate", subscribe); return () => window.removeEventListener("popstate", subscribe); }, []); return {props.children} }