useRoutes
Hook version of<Routes> that uses objects instead of components. These objects have the same properties as the component props. The return value of useRoutes is either a valid React element you can use to render the route tree, or null if nothing matched.
Signature
Parameters
RouteObject[]
required
An array of route objects that define the route hierarchy.
Partial<Location> | string
An optional location object or pathname string to use instead of the current location. Useful for testing or SSR.
Returns
React.ReactElement | null
A React element to render the matched route, or
null if no routes matched.Usage
Basic usage
With data loading
Dynamic routes
With custom location
Error boundaries
Common Patterns
Route configuration file
Nested routing
Lazy loading routes
Protected routes
Index routes
Wildcard routes
Route handles
Comparison with Routes
Type Safety
Typed routes
Custom route type
Important Notes
Must be inside Router
useRoutes must be called within a <Router> component:
Location override
When overriding location, all parent routes must match:Related
Routes- Component versionRoute- Individual route componentcreateBrowserRouter- Create router with data APIsuseMatches- Get active route matches