react-router-dom

  • Version 6.22.3
  • Published
  • 877 kB
  • 2 dependencies
  • MIT license

Install

npm i react-router-dom
yarn add react-router-dom
pnpm add react-router-dom

Overview

Declarative routing for React web applications

Index

Variables

variable Form

const Form: React.ForwardRefExoticComponent<any>;
  • A @remix-run/router-aware <form>. It behaves like a normal form except that the interaction with the server is with fetch instead of new document requests, allowing components to add nicer UX to the page as the form is submitted and returns with data.

const Link: React.ForwardRefExoticComponent<any>;
  • The public API for rendering a history-aware <a>.

const NavLink: React.ForwardRefExoticComponent<any>;
  • A <Link> wrapper that knows if it's "active" or not.

variable UNSAFE_FetchersContext

const UNSAFE_FetchersContext: React.Context<FetchersContextObject>;

    variable UNSAFE_ViewTransitionContext

    const UNSAFE_ViewTransitionContext: React.Context<ViewTransitionContextObject>;

      Functions

      function BrowserRouter

      BrowserRouter: ({
      basename,
      children,
      future,
      window,
      }: BrowserRouterProps) => React.JSX.Element;
      • A <Router> for use in web browsers. Provides the cleanest URLs.

      function createBrowserRouter

      createBrowserRouter: (
      routes: RouteObject[],
      opts?: DOMRouterOpts
      ) => RemixRouter;

        function createHashRouter

        createHashRouter: (routes: RouteObject[], opts?: DOMRouterOpts) => RemixRouter;

          function createSearchParams

          createSearchParams: (init?: URLSearchParamsInit) => URLSearchParams;
          • Creates a URLSearchParams object using the given initializer.

            This is identical to new URLSearchParams(init) except it also supports arrays as values in the object form of the initializer instead of just strings. This is convenient when you need multiple values for a given key, but don't want to use an array initializer.

            For example, instead of:

            let searchParams = new URLSearchParams([ ['sort', 'name'], ['sort', 'price'] ]);

            you can do:

            let searchParams = createSearchParams({ sort: ['name', 'price'] });

          function HashRouter

          HashRouter: ({
          basename,
          children,
          future,
          window,
          }: HashRouterProps) => React.JSX.Element;
          • A <Router> for use in web browsers. Stores the location in the hash portion of the URL so it is not sent to the server.

          function RouterProvider

          RouterProvider: ({
          fallbackElement,
          router,
          future,
          }: RouterProviderProps) => React.ReactElement;
          • Given a Remix Router instance, render the appropriate UI

          function ScrollRestoration

          ScrollRestoration: typeof ScrollRestoration;
          • This component will emulate the browser's scroll restoration on location changes.

          function UNSAFE_useScrollRestoration

          UNSAFE_useScrollRestoration: ({
          getKey,
          storageKey,
          }?: {
          getKey?: GetScrollRestorationKeyFunction;
          storageKey?: string;
          }) => void;
          • When rendered inside a RouterProvider, will restore scroll positions on navigations

          function unstable_HistoryRouter

          unstable_HistoryRouter: typeof HistoryRouter;
          • A <Router> that accepts a pre-instantiated history object. It's important to note that using your own history object is highly discouraged and may add two versions of the history library to your bundles unless you use the same version of the history library that React Router uses internally.

          function unstable_usePrompt

          unstable_usePrompt: ({
          when,
          message,
          }: {
          when: boolean | BlockerFunction;
          message: string;
          }) => void;
          • Wrapper around useBlocker to show a window.confirm prompt to users instead of building a custom UI with useBlocker.

            Warning: This has *a lot of rough edges* and behaves very differently (and very incorrectly in some cases) across browsers if user click addition back/forward navigations while the confirm is open. Use at your own risk.

          function unstable_useViewTransitionState

          unstable_useViewTransitionState: (
          to: To,
          opts?: { relative?: RelativeRoutingType }
          ) => boolean;
          • Return a boolean indicating if there is an active view transition to the given href. You can use this value to render CSS classes or viewTransitionName styles onto your elements

            Parameter href

            The destination href

            Parameter

            [opts.relative] Relative routing type ("route" | "path")

          function useBeforeUnload

          useBeforeUnload: (
          callback: (event: BeforeUnloadEvent) => any,
          options?: { capture?: boolean }
          ) => void;
          • Setup a callback to be fired on the window's beforeunload event. This is useful for saving some data to window.localStorage just before the page refreshes.

            Note: The callback argument should be a function created with React.useCallback().

          function useFetcher

          useFetcher: <TData = any>({
          key,
          }?: {
          key?: string;
          }) => FetcherWithComponents<TData>;
          • Interacts with route loaders and actions without causing a navigation. Great for any interaction that stays on the same page.

          function useFetchers

          useFetchers: () => (Fetcher & { key: string })[];
          • Provides all fetchers currently on the page. Useful for layouts and parent routes that need to provide pending/optimistic UI regarding the fetch.

          function useFormAction

          useFormAction: (
          action?: string,
          { relative }?: { relative?: RelativeRoutingType }
          ) => string;

            function useLinkClickHandler

            useLinkClickHandler: <E extends Element = HTMLAnchorElement>(
            to: To,
            {
            target,
            replace: replaceProp,
            state,
            preventScrollReset,
            relative,
            unstable_viewTransition,
            }?: {
            target?: React.HTMLAttributeAnchorTarget;
            replace?: boolean;
            state?: any;
            preventScrollReset?: boolean;
            relative?: RelativeRoutingType;
            unstable_viewTransition?: boolean;
            }
            ) => (event: React.MouseEvent<E, MouseEvent>) => void;
            • Handles the click behavior for router <Link> components. This is useful if you need to create custom <Link> components with the same click behavior we use in our exported <Link>.

            function useSearchParams

            useSearchParams: (
            defaultInit?: URLSearchParamsInit
            ) => [URLSearchParams, SetURLSearchParams];
            • A convenient wrapper for reading and writing search parameters via the URLSearchParams interface.

            function useSubmit

            useSubmit: () => SubmitFunction;
            • Returns a function that may be used to programmatically submit a form (or some arbitrary data) to the server.

            Interfaces

            interface BrowserRouterProps

            interface BrowserRouterProps {}

              property basename

              basename?: string;

                property children

                children?: React.ReactNode;

                  property future

                  future?: Partial<FutureConfig>;

                    property window

                    window?: Window;

                      interface FetcherFormProps

                      interface FetcherFormProps extends React.FormHTMLAttributes<HTMLFormElement> {}

                        property action

                        action?: string;
                        • Normal <form action> but supports React Router's relative paths.

                        property encType

                        encType?:
                        | 'application/x-www-form-urlencoded'
                        | 'multipart/form-data'
                        | 'text/plain';
                        • <form encType> - enhancing beyond the normal string type and limiting to the built-in browser supported values

                        property method

                        method?: HTMLFormMethod;
                        • The HTTP verb to use when the form is submit. Supports "get", "post", "put", "delete", "patch".

                        property onSubmit

                        onSubmit?: React.FormEventHandler<HTMLFormElement>;
                        • A function to call when the form is submitted. If you call event.preventDefault() then this form will not do anything.

                        property preventScrollReset

                        preventScrollReset?: boolean;
                        • Prevent the scroll position from resetting to the top of the viewport on completion of the navigation when using the component

                        property relative

                        relative?: RelativeRoutingType;
                        • Determines whether the form action is relative to the route hierarchy or the pathname. Use this if you want to opt out of navigating the route hierarchy and want to instead route based on /-delimited URL segments

                        interface FetcherSubmitFunction

                        interface FetcherSubmitFunction {}
                        • Submits a fetcher <form> to the server without reloading the page.

                        call signature

                        (target: SubmitTarget, options?: Omit<SubmitOptions, 'replace' | 'state'>): void;

                          interface FormProps

                          interface FormProps extends FetcherFormProps {}

                            property fetcherKey

                            fetcherKey?: string;
                            • Indicate a specific fetcherKey to use when using navigate=false

                            property navigate

                            navigate?: boolean;
                            • navigate=false will use a fetcher instead of a navigation

                            property reloadDocument

                            reloadDocument?: boolean;
                            • Forces a full document navigation instead of a fetch.

                            property replace

                            replace?: boolean;
                            • Replaces the current entry in the browser history stack when the form navigates. Use this if you don't want the user to be able to click "back" to the page with the form on it.

                            property state

                            state?: any;
                            • State object to add to the history stack entry for this navigation

                            property unstable_viewTransition

                            unstable_viewTransition?: boolean;
                            • Enable view transitions on this Form navigation

                            interface HashRouterProps

                            interface HashRouterProps {}

                              property basename

                              basename?: string;

                                property children

                                children?: React.ReactNode;

                                  property future

                                  future?: Partial<FutureConfig>;

                                    property window

                                    window?: Window;

                                      interface HistoryRouterProps

                                      interface HistoryRouterProps {}

                                        property basename

                                        basename?: string;

                                          property children

                                          children?: React.ReactNode;

                                            property future

                                            future?: FutureConfig;

                                              property history

                                              history: History;

                                                interface LinkProps

                                                interface LinkProps
                                                extends Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'href'> {}

                                                  property preventScrollReset

                                                  preventScrollReset?: boolean;

                                                    property relative

                                                    relative?: RelativeRoutingType;

                                                      property reloadDocument

                                                      reloadDocument?: boolean;

                                                        property replace

                                                        replace?: boolean;

                                                          property state

                                                          state?: any;

                                                            property to

                                                            to: To;

                                                              property unstable_viewTransition

                                                              unstable_viewTransition?: boolean;
                                                                interface NavLinkProps extends Omit<LinkProps, 'className' | 'style' | 'children'> {}
                                                                  caseSensitive?: boolean;
                                                                    children?: React.ReactNode | ((props: NavLinkRenderProps) => React.ReactNode);
                                                                      className?: string | ((props: NavLinkRenderProps) => string | undefined);
                                                                        end?: boolean;
                                                                          style?:
                                                                          | React.CSSProperties
                                                                          | ((props: NavLinkRenderProps) => React.CSSProperties | undefined);

                                                                            interface ScrollRestorationProps

                                                                            interface ScrollRestorationProps {}

                                                                              property getKey

                                                                              getKey?: GetScrollRestorationKeyFunction;

                                                                                property storageKey

                                                                                storageKey?: string;

                                                                                  interface SubmitFunction

                                                                                  interface SubmitFunction {}
                                                                                  • Submits a HTML <form> to the server without reloading the page.

                                                                                  call signature

                                                                                  (
                                                                                  /**
                                                                                  * Specifies the `<form>` to be submitted to the server, a specific
                                                                                  * `<button>` or `<input type="submit">` to use to submit the form, or some
                                                                                  * arbitrary data to submit.
                                                                                  *
                                                                                  * Note: When using a `<button>` its `name` and `value` will also be
                                                                                  * included in the form data that is submitted.
                                                                                  */
                                                                                  target: SubmitTarget,
                                                                                  /**
                                                                                  * Options that override the `<form>`'s own attributes. Required when
                                                                                  * submitting arbitrary data without a backing `<form>`.
                                                                                  */
                                                                                  options?: SubmitOptions
                                                                                  ): void;

                                                                                    interface SubmitOptions

                                                                                    interface SubmitOptions {}

                                                                                      property action

                                                                                      action?: string;
                                                                                      • The action URL path used to submit the form. Overrides <form action>. Defaults to the path of the current route.

                                                                                      property encType

                                                                                      encType?: FormEncType;
                                                                                      • The encoding used to submit the form. Overrides <form encType>. Defaults to "application/x-www-form-urlencoded".

                                                                                      property fetcherKey

                                                                                      fetcherKey?: string;
                                                                                      • Indicate a specific fetcherKey to use when using navigate=false

                                                                                      property method

                                                                                      method?: HTMLFormMethod;
                                                                                      • The HTTP method used to submit the form. Overrides <form method>. Defaults to "GET".

                                                                                      property navigate

                                                                                      navigate?: boolean;
                                                                                      • navigate=false will use a fetcher instead of a navigation

                                                                                      property preventScrollReset

                                                                                      preventScrollReset?: boolean;
                                                                                      • In browser-based environments, prevent resetting scroll after this navigation when using the component

                                                                                      property relative

                                                                                      relative?: RelativeRoutingType;
                                                                                      • Determines whether the form action is relative to the route hierarchy or the pathname. Use this if you want to opt out of navigating the route hierarchy and want to instead route based on /-delimited URL segments

                                                                                      property replace

                                                                                      replace?: boolean;
                                                                                      • Set true to replace the current entry in the browser's history stack instead of creating a new one (i.e. stay on "the same page"). Defaults to false.

                                                                                      property state

                                                                                      state?: any;
                                                                                      • State object to add to the history stack entry for this navigation

                                                                                      property unstable_flushSync

                                                                                      unstable_flushSync?: boolean;
                                                                                      • Enable flushSync for this navigation's state updates

                                                                                      property unstable_viewTransition

                                                                                      unstable_viewTransition?: boolean;
                                                                                      • Enable view transitions on this submission navigation

                                                                                      Type Aliases

                                                                                      type FetcherWithComponents

                                                                                      type FetcherWithComponents<TData> = Fetcher<TData> & {
                                                                                      Form: React.ForwardRefExoticComponent<
                                                                                      FetcherFormProps & React.RefAttributes<HTMLFormElement>
                                                                                      >;
                                                                                      submit: FetcherSubmitFunction;
                                                                                      load: (
                                                                                      href: string,
                                                                                      opts?: {
                                                                                      unstable_flushSync?: boolean;
                                                                                      }
                                                                                      ) => void;
                                                                                      };

                                                                                        type ParamKeyValuePair

                                                                                        type ParamKeyValuePair = [string, string];

                                                                                          type SetURLSearchParams

                                                                                          type SetURLSearchParams = (
                                                                                          nextInit?:
                                                                                          | URLSearchParamsInit
                                                                                          | ((prev: URLSearchParams) => URLSearchParamsInit),
                                                                                          navigateOpts?: NavigateOptions
                                                                                          ) => void;

                                                                                            type URLSearchParamsInit

                                                                                            type URLSearchParamsInit =
                                                                                            | string
                                                                                            | ParamKeyValuePair[]
                                                                                            | Record<string, string | string[]>
                                                                                            | URLSearchParams;

                                                                                              Namespaces

                                                                                              namespace global

                                                                                              namespace global {}

                                                                                                interface Document

                                                                                                interface Document {}

                                                                                                  method startViewTransition

                                                                                                  startViewTransition: (cb: () => Promise<void> | void) => ViewTransition;

                                                                                                    namespace ScrollRestoration

                                                                                                    namespace ScrollRestoration {}

                                                                                                      variable displayName

                                                                                                      var displayName: string;

                                                                                                        namespace unstable_HistoryRouter

                                                                                                        namespace unstable_HistoryRouter {}

                                                                                                          variable displayName

                                                                                                          var displayName: string;

                                                                                                            Package Files (2)

                                                                                                            Dependencies (2)

                                                                                                            Dev Dependencies (2)

                                                                                                            Peer Dependencies (2)

                                                                                                            Badge

                                                                                                            To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                                                                            You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/react-router-dom.

                                                                                                            • Markdown
                                                                                                              [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/react-router-dom)
                                                                                                            • HTML
                                                                                                              <a href="https://www.jsdocs.io/package/react-router-dom"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>