Skip to main content

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. The <Link> component is the primary way to navigate:
<NavLink> adds active styling automatically:

useNavigate Hook

For programmatic navigation:
From lib/hooks.tsx:379, the navigate function signature:
Declarative navigation (useful in class components):

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

From lib/hooks.tsx:301:
Track navigation status:
Navigation states: The Navigation type from lib/router/router.ts:

Form Navigation

Forms trigger navigation with data mutations:

Form Methods

Blocking Navigation

Prevent navigation during unsaved changes:
Blocker states:

Location

Access current location:
Location object structure from 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:
Customize scroll behavior:

Best Practices

  1. Use Link for internal navigation - Better performance and user experience
  2. Use navigate() for post-mutation redirects - After forms, login, etc.
  3. Prefer replace for redirects - Especially for auth redirects
  4. Use state for non-URL data - Modal backgrounds, scroll positions
  5. Block navigation carefully - Only for critical unsaved changes
  6. Handle missing history - Check if user can actually go back
  7. Use NavLink for navigation menus - Automatic active states
  8. Leverage view transitions - For smooth page transitions