Navigation Concepts
React Router provides multiple ways to navigate between routes, from declarative components to imperative APIs. Understanding when and how to use each method is key to building intuitive applications.Navigation Methods
Link Component
The<Link> component is the primary way to navigate:
NavLink Component
<NavLink> adds active styling automatically:
useNavigate Hook
For programmatic navigation:lib/hooks.tsx:379, the navigate function signature:
Navigate Component
Declarative navigation (useful in class components):Navigation Options
Replace vs Push
State
Pass state that doesn’t belong in the URL:Prevent Scroll Reset
In Data/Framework modes:View Transitions
Enable view transitions API:Flush Sync
Force synchronous DOM updates:Relative Navigation
Route-Relative (Default)
Relative to the route hierarchy:Path-Relative
Relative to URL path segments:Absolute Navigation
History Navigation
Go Back/Forward
Caution with Delta Navigation
Fromlib/hooks.tsx:301:
Navigation State
Track navigation status:Navigation type from lib/router/router.ts:
Form Navigation
Forms trigger navigation with data mutations:Form Methods
Blocking Navigation
Prevent navigation during unsaved changes:Location
Access current location:lib/router/history.ts:
Search Params
Work with query strings:Basename
Mount app at a subdirectory:Hash Navigation
Navigate to anchor links:External Navigation
For external links, use regular<a> tags:
Scroll Restoration
Automatic scroll position restoration:Navigation Lifecycle
Best Practices
- Use Link for internal navigation - Better performance and user experience
- Use navigate() for post-mutation redirects - After forms, login, etc.
- Prefer replace for redirects - Especially for auth redirects
- Use state for non-URL data - Modal backgrounds, scroll positions
- Block navigation carefully - Only for critical unsaved changes
- Handle missing history - Check if user can actually go back
- Use NavLink for navigation menus - Automatic active states
- Leverage view transitions - For smooth page transitions