You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In a few Remix projects, I added custom route handles for some features (such as breadcrumbs, custom back navigations, ...). While implementing these, I noticed significant boilerplate for adding custom route handles.
After playing around, I came to a (more or less) generic solution which can look something like this:
import{useMemo}from"react";import{typeParams,typeRouterState,typeuseLoaderData,useLocation,useMatches,}from"react-router";import{HandleConventionArguments}from"remix-utils";exporttypeRouteHandle<Keyextendsstring,Return,Loader=unknown>={[KinKey]: (args: HandleConventionArguments<ReturnType<typeofuseLoaderData<Loader>>>,)=>Return;};exporttypeRouteHandleData<Return>={id: string;location: ReturnType<typeofuseLocation>;data: Return;};typeHandleKey<T>=TextendsRouteHandle<
infer Key,
infer _Return,
infer _Loader>
? Key
: never;typeHandleReturn<T>=TextendsRouteHandle<
infer _Key,
infer Return,
infer _Loader>
? Return
: never;exportfunctionuseHandle<Handle,KeyextendsHandleKey<Handle>=HandleKey<Handle>,ReturnextendsHandleReturn<Handle>=HandleReturn<Handle>,>(key: Key): RouteHandleData<Return>[]{constmatches=useMatches();constlocation=useLocation();returnuseMemo(()=>{// initialize with configurationlethandleDataOfRoutes: RouteHandleData<Return>[]=[];// walk through all matches, prioritizing leaf nodes (i.e., they run last)for(leti=0;i<matches.length;i++){constmatch=matches[i];constrouteModule=match.handleasRouteHandle<Key,Return>|undefined;if(!routeModule){continue;}constfn=routeModule[key];if(!fn){continue;}consthandleData=fn({id: match.id,data: match.data,params: match.params,
location,parentsData: matches.slice(0,i).map((match)=>match.data),
matches,});handleDataOfRoutes=[
...handleDataOfRoutes,{id: match.id,
location,data: handleData,},];}returnhandleDataOfRoutes;},[key,matches,location]);}
Example use case:
exportinterfaceNavigationConfiguration{back?: {url: string;text: string;};}exporttypeNavigationRouteHandle<Loader=unknown>=RouteHandle<"navigation",Partial<NavigationConfiguration>,Loader>;exportfunctionNavigation({ children }: PropsWithChildren){consthandleData=useHandle<NavigationRouteHandle>("navigation");constnavigationConfiguration=useMemo(()=>{letmergedNavigationConfiguration: NavigationConfiguration={};for(constdataofhandleData){mergedNavigationConfiguration={
...mergedNavigationConfiguration,
...data.data,};}returnmergedNavigationConfiguration;},[handleData]);return(<NavigationContext.Providervalue={navigationConfiguration}>{children}</NavigationContext.Provider>);}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi!
In a few Remix projects, I added custom route handles for some features (such as breadcrumbs, custom back navigations, ...). While implementing these, I noticed significant boilerplate for adding custom route handles.
After playing around, I came to a (more or less) generic solution which can look something like this:
Example use case:
... and in routes:
Would you consider adding such a utility to remix-utils?
Beta Was this translation helpful? Give feedback.
All reactions