hooks.d.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import * as React from "react";
  2. import type { Blocker, BlockerFunction, Location, ParamParseKey, Params, Path, PathMatch, PathPattern, Router as RemixRouter, To } from "@remix-run/router";
  3. import { Action as NavigationType } from "@remix-run/router";
  4. import type { NavigateOptions, RouteContextObject, RouteMatch, RouteObject, RelativeRoutingType } from "./context";
  5. /**
  6. * Returns the full href for the given "to" value. This is useful for building
  7. * custom links that are also accessible and preserve right-click behavior.
  8. *
  9. * @see https://reactrouter.com/hooks/use-href
  10. */
  11. export declare function useHref(to: To, { relative }?: {
  12. relative?: RelativeRoutingType;
  13. }): string;
  14. /**
  15. * Returns true if this component is a descendant of a <Router>.
  16. *
  17. * @see https://reactrouter.com/hooks/use-in-router-context
  18. */
  19. export declare function useInRouterContext(): boolean;
  20. /**
  21. * Returns the current location object, which represents the current URL in web
  22. * browsers.
  23. *
  24. * Note: If you're using this it may mean you're doing some of your own
  25. * "routing" in your app, and we'd like to know what your use case is. We may
  26. * be able to provide something higher-level to better suit your needs.
  27. *
  28. * @see https://reactrouter.com/hooks/use-location
  29. */
  30. export declare function useLocation(): Location;
  31. /**
  32. * Returns the current navigation action which describes how the router came to
  33. * the current location, either by a pop, push, or replace on the history stack.
  34. *
  35. * @see https://reactrouter.com/hooks/use-navigation-type
  36. */
  37. export declare function useNavigationType(): NavigationType;
  38. /**
  39. * Returns a PathMatch object if the given pattern matches the current URL.
  40. * This is useful for components that need to know "active" state, e.g.
  41. * <NavLink>.
  42. *
  43. * @see https://reactrouter.com/hooks/use-match
  44. */
  45. export declare function useMatch<ParamKey extends ParamParseKey<Path>, Path extends string>(pattern: PathPattern<Path> | Path): PathMatch<ParamKey> | null;
  46. /**
  47. * The interface for the navigate() function returned from useNavigate().
  48. */
  49. export interface NavigateFunction {
  50. (to: To, options?: NavigateOptions): void;
  51. (delta: number): void;
  52. }
  53. /**
  54. * Returns an imperative method for changing the location. Used by <Link>s, but
  55. * may also be used by other elements to change the location.
  56. *
  57. * @see https://reactrouter.com/hooks/use-navigate
  58. */
  59. export declare function useNavigate(): NavigateFunction;
  60. /**
  61. * Returns the context (if provided) for the child route at this level of the route
  62. * hierarchy.
  63. * @see https://reactrouter.com/hooks/use-outlet-context
  64. */
  65. export declare function useOutletContext<Context = unknown>(): Context;
  66. /**
  67. * Returns the element for the child route at this level of the route
  68. * hierarchy. Used internally by <Outlet> to render child routes.
  69. *
  70. * @see https://reactrouter.com/hooks/use-outlet
  71. */
  72. export declare function useOutlet(context?: unknown): React.ReactElement | null;
  73. /**
  74. * Returns an object of key/value pairs of the dynamic params from the current
  75. * URL that were matched by the route path.
  76. *
  77. * @see https://reactrouter.com/hooks/use-params
  78. */
  79. export declare function useParams<ParamsOrKey extends string | Record<string, string | undefined> = string>(): Readonly<[
  80. ParamsOrKey
  81. ] extends [string] ? Params<ParamsOrKey> : Partial<ParamsOrKey>>;
  82. /**
  83. * Resolves the pathname of the given `to` value against the current location.
  84. *
  85. * @see https://reactrouter.com/hooks/use-resolved-path
  86. */
  87. export declare function useResolvedPath(to: To, { relative }?: {
  88. relative?: RelativeRoutingType;
  89. }): Path;
  90. /**
  91. * Returns the element of the route that matched the current location, prepared
  92. * with the correct context to render the remainder of the route tree. Route
  93. * elements in the tree must render an <Outlet> to render their child route's
  94. * element.
  95. *
  96. * @see https://reactrouter.com/hooks/use-routes
  97. */
  98. export declare function useRoutes(routes: RouteObject[], locationArg?: Partial<Location> | string): React.ReactElement | null;
  99. declare type RenderErrorBoundaryProps = React.PropsWithChildren<{
  100. location: Location;
  101. error: any;
  102. component: React.ReactNode;
  103. routeContext: RouteContextObject;
  104. }>;
  105. declare type RenderErrorBoundaryState = {
  106. location: Location;
  107. error: any;
  108. };
  109. export declare class RenderErrorBoundary extends React.Component<RenderErrorBoundaryProps, RenderErrorBoundaryState> {
  110. constructor(props: RenderErrorBoundaryProps);
  111. static getDerivedStateFromError(error: any): {
  112. error: any;
  113. };
  114. static getDerivedStateFromProps(props: RenderErrorBoundaryProps, state: RenderErrorBoundaryState): {
  115. error: any;
  116. location: Location;
  117. };
  118. componentDidCatch(error: any, errorInfo: any): void;
  119. render(): string | number | boolean | React.ReactFragment | JSX.Element | null | undefined;
  120. }
  121. export declare function _renderMatches(matches: RouteMatch[] | null, parentMatches?: RouteMatch[], dataRouterState?: RemixRouter["state"]): React.ReactElement | null;
  122. /**
  123. * Returns the current navigation, defaulting to an "idle" navigation when
  124. * no navigation is in progress
  125. */
  126. export declare function useNavigation(): import("@remix-run/router").Navigation;
  127. /**
  128. * Returns a revalidate function for manually triggering revalidation, as well
  129. * as the current state of any manual revalidations
  130. */
  131. export declare function useRevalidator(): {
  132. revalidate: () => void;
  133. state: import("@remix-run/router").RevalidationState;
  134. };
  135. /**
  136. * Returns the active route matches, useful for accessing loaderData for
  137. * parent/child routes or the route "handle" property
  138. */
  139. export declare function useMatches(): {
  140. id: string;
  141. pathname: string;
  142. params: Params<string>;
  143. data: unknown;
  144. handle: unknown;
  145. }[];
  146. /**
  147. * Returns the loader data for the nearest ancestor Route loader
  148. */
  149. export declare function useLoaderData(): unknown;
  150. /**
  151. * Returns the loaderData for the given routeId
  152. */
  153. export declare function useRouteLoaderData(routeId: string): unknown;
  154. /**
  155. * Returns the action data for the nearest ancestor Route action
  156. */
  157. export declare function useActionData(): unknown;
  158. /**
  159. * Returns the nearest ancestor Route error, which could be a loader/action
  160. * error or a render error. This is intended to be called from your
  161. * errorElement to display a proper error message.
  162. */
  163. export declare function useRouteError(): unknown;
  164. /**
  165. * Returns the happy-path data from the nearest ancestor <Await /> value
  166. */
  167. export declare function useAsyncValue(): unknown;
  168. /**
  169. * Returns the error from the nearest ancestor <Await /> value
  170. */
  171. export declare function useAsyncError(): unknown;
  172. /**
  173. * Allow the application to block navigations within the SPA and present the
  174. * user a confirmation dialog to confirm the navigation. Mostly used to avoid
  175. * using half-filled form data. This does not handle hard-reloads or
  176. * cross-origin navigations.
  177. */
  178. export declare function useBlocker(shouldBlock: boolean | BlockerFunction): Blocker;
  179. export {};