import React , { createContext , useContext } from 'react' ; import { Route , Router } from './types' ; const RouterContext = createContext < Router < Route [ ] > | undefined > ( undefined ) ; export const RouterContextProvider = ( { router , children , } ) => < RouterContext . Provider value = { router } > { children } < /RouterContext.Provider>; export function useRouter ( ) { const router = useContext ( RouterContext ) ; if ( ! router ) { throw new Error ( 'Router not found in context' ) ; } return router ; }