import React , { createContext , useContext } from 'react' ; import { OutletContextProvider } from './outlet' ; import { RouteMatch } from './types' ; const CurrentRouteContext = createContext < { match : RouteMatch ; element : React . ReactElement ; } | undefined > ( undefined ) ; export const CurrentRouteContextProvider = ( { match , element , children , } ) => { return ( < CurrentRouteContext . Provider value = { { match , element } } > < OutletContextProvider element = { element } > { children } < /OutletContextProvider> < /CurrentRouteContext.Provider>); } ; export const useCurrentRoute = ( ) => { const currentRoute = useContext ( CurrentRouteContext ) ; if ( ! currentRoute ) { throw new Error ( 'No match was found in context' ) ; } return currentRoute ; } ;