1.15.3 • Published 21 hours ago

@remix-run/router v1.15.3

Weekly downloads
-
License
MIT
Repository
github
Last release
21 hours ago

Router

The @remix-run/router package is the heart of React Router and provides all the core functionality for routing, data loading, data mutations, and navigation.

If you're using React Router, you should never import anything directly from the @remix-run/router or react-router packages, but you should have everything you need in either react-router-dom or react-router-native. Both of those packages re-export everything from @remix-run/router and react-router.

API

A Router instance can be created using createRouter:

// Create and initialize a router.  "initialize" contains all side effects
// including history listeners and kicking off the initial data fetch
let router = createRouter({
  // Routes array using react-router RouteObject's
  routes,
  // History instance
  history,
  // Optional hydration data for SSR apps
  hydrationData?: HydrationState;
}).initialize()

Internally, the Router represents the state in an object of the following format, which is available through router.state. You can also register a subscriber of the signature (state: RouterState) => void to execute when the state updates via router.subscribe();

interface RouterState {
  // The `history` action of the most recently completed navigation
  historyAction: Action;
  // The current location of the router.  During a navigation this reflects
  // the "old" location and is updated upon completion of the navigation
  location: Location;
  // The current set of route matches
  matches: DataRouteMatch[];
  // False during the initial data load, true once we have our initial data
  initialized: boolean;
  // The state of the current navigation
  navigation: Navigation;
  // The state of an in-progress router.revalidate() calls
  revalidation: RevalidationState;
  // Scroll position to restore to for the active Location, false if we
  // should not restore,m or null if we don't have a saved position
  // Note: must be enabled via router.enableScrollRestoration()
  restoreScrollPosition: number | false | null;
  // Proxied `resetScroll` value passed to router.navigate() (default true)
  resetScrollPosition: boolean;
  // Data from the loaders for the current matches
  loaderData: RouteData;
  // Data from the action for the current matches
  actionData: RouteData | null;
  // Errors thrown from loaders/actions for the current matches
  errors: RouteData | null;
  // Map of all active fetchers
  fetchers: Map<string, Fetcher>;
}

Navigations

All navigations are done through the router.navigate API which is overloaded to support different types of navigations:

// Link navigation (pushes onto the history stack by default)
router.navigate("/page");

// Link navigation (replacing the history stack)
router.navigate("/page", { replace: true });

// Pop navigation (moving backward/forward in the history stack)
router.navigate(-1);

// Form submission navigation
let formData = new FormData();
formData.append(key, value);
router.navigate("/page", {
  formMethod: "post",
  formData,
});

Fetchers

Fetchers are a mechanism to call loaders/actions without triggering a navigation, and are done through the router.fetch() API. All fetch calls require a unique key to identify the fetcher.

// Execute the loader for /page
router.fetch("key", "/page");

// Submit to the action for /page
let formData = new FormData();
formData.append(key, value);
router.fetch("key", "/page", {
  formMethod: "post",
  formData,
});

Revalidation

By default, active loaders will revalidate after any navigation or fetcher mutation. If you need to kick off a revalidation for other use-cases, you can use router.revalidate() to re-execute all active loaders.

1.16.0-pre.0

21 hours ago

1.15.3

21 days ago

1.15.3-pre.0

23 days ago

1.15.2

29 days ago

1.15.2-pre.0

30 days ago

1.15.1

1 month ago

1.15.1-pre.0

1 month ago

1.15.0

2 months ago

1.15.0-pre.0

2 months ago

1.14.2

3 months ago

1.14.2-pre.0

3 months ago

1.14.1

3 months ago

1.14.1-pre.0

3 months ago

1.14.0

4 months ago

1.14.0-pre.1

4 months ago

1.14.0-pre.0

4 months ago

1.11.0-pre.0

5 months ago

1.11.0-pre.1

5 months ago

1.9.0

7 months ago

1.9.0-pre.2

7 months ago

1.12.0-pre.0

4 months ago

1.9.0-pre.1

7 months ago

1.9.0-pre.0

7 months ago

1.7.1-pre.0

9 months ago

1.10.0

5 months ago

1.12.0

4 months ago

1.8.0

8 months ago

1.13.1-pre.0

4 months ago

1.7.2-pre.0

9 months ago

1.7.2-pre.1

8 months ago

1.13.0-pre.0

4 months ago

1.11.0

5 months ago

1.13.1

4 months ago

1.13.0

4 months ago

1.7.2

8 months ago

1.7.1

9 months ago

1.8.0-pre.0

8 months ago

1.10.0-pre.0

6 months ago

1.6.3

10 months ago

1.6.2

11 months ago

1.6.1

11 months ago

1.6.2-pre.0

11 months ago

1.7.0-pre.0

9 months ago

1.6.1-pre.0

11 months ago

1.7.0

9 months ago

1.6.3-pre.0

10 months ago

1.5.0-pre.1

1 year ago

1.5.0-pre.0

1 year ago

1.5.0-pre.2

1 year ago

1.6.0

11 months ago

1.4.0

1 year ago

1.4.0-pre.0

1 year ago

1.5.0

12 months ago

1.6.0-pre.0

11 months ago

1.3.3

1 year ago

1.3.3-pre.1

1 year ago

1.3.3-pre.0

1 year ago

1.3.2

1 year ago

1.3.2-pre.0

1 year ago

1.2.0

1 year ago

1.1.0

1 year ago

1.3.0-pre.2

1 year ago

1.3.0-pre.3

1 year ago

1.3.0-pre.0

1 year ago

1.3.0-pre.1

1 year ago

1.0.5

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.1

1 year ago

1.2.0-pre.0

1 year ago

1.3.1-pre.0

1 year ago

1.2.1-pre.0

1 year ago

1.2.1-pre.1

1 year ago

1.1.0-pre.1

1 year ago

1.1.0-pre.0

1 year ago

1.0.5-pre.0

1 year ago

1.0.5-pre.1

1 year ago

1.0.5-pre.2

1 year ago

1.0.2

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2-pre.0

1 year ago

1.0.4-pre.1

1 year ago

1.0.4-pre.0

1 year ago

1.0.3-pre.1

1 year ago

1.0.3-pre.0

1 year ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.1-pre.0

2 years ago

0.2.0-pre.10

2 years ago

0.2.0-pre.9

2 years ago

0.2.0-pre.5

2 years ago

0.2.0-pre.4

2 years ago

0.2.0-pre.3

2 years ago

0.2.0-pre.8

2 years ago

0.2.0-pre.7

2 years ago

0.2.0-pre.6

2 years ago

0.2.0-pre.2

2 years ago

0.2.0-pre.1

2 years ago

0.2.0-pre.0

2 years ago

0.1.0

2 years ago