@tanstack/react-router
Version:
Modern and scalable routing for React applications
140 lines (139 loc) • 4.85 kB
JavaScript
import { useRouter } from "./useRouter.js";
import { useMatch } from "./useMatch.js";
import { useLoaderData } from "./useLoaderData.js";
import { useLoaderDeps } from "./useLoaderDeps.js";
import { useParams } from "./useParams.js";
import { useSearch } from "./useSearch.js";
import { useNavigate } from "./useNavigate.js";
import { useRouteContext } from "./useRouteContext.js";
import { createRoute } from "./route.js";
//#region src/fileRoute.ts
/**
* Creates a file-based Route factory for a given path.
*
* Used by TanStack Router's file-based routing to associate a file with a
* route. The returned function accepts standard route options. In normal usage
* the `path` string is inserted and maintained by the `tsr` generator.
*
* @param path File path literal for the route (usually auto-generated).
* @returns A function that accepts Route options and returns a Route instance.
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/createFileRouteFunction
*/
function createFileRoute(path) {
if (typeof path === "object") return new FileRoute(path, { silent: true }).createRoute(path);
return new FileRoute(path, { silent: true }).createRoute;
}
/**
@deprecated It's no longer recommended to use the `FileRoute` class directly.
Instead, use `createFileRoute('/path/to/file')(options)` to create a file route.
*/
var FileRoute = class {
constructor(path, _opts) {
this.path = path;
this.createRoute = (options) => {
if (process.env.NODE_ENV !== "production") {
if (!this.silent) console.warn("Warning: FileRoute is deprecated and will be removed in the next major version. Use the createFileRoute(path)(options) function instead.");
}
const route = createRoute(options);
route.isRoot = false;
return route;
};
this.silent = _opts?.silent;
}
};
/**
@deprecated It's recommended not to split loaders into separate files.
Instead, place the loader function in the main route file via `createFileRoute`.
*/
function FileRouteLoader(_path) {
if (process.env.NODE_ENV !== "production") console.warn(`Warning: FileRouteLoader is deprecated and will be removed in the next major version. Please place the loader function in the the main route file, inside the \`createFileRoute('/path/to/file')(options)\` options`);
return (loaderFn) => loaderFn;
}
var LazyRoute = class {
constructor(opts) {
this.useMatch = (opts) => {
return useMatch({
select: opts?.select,
from: this.options.id,
structuralSharing: opts?.structuralSharing
});
};
this.useRouteContext = (opts) => {
return useRouteContext({
...opts,
from: this.options.id
});
};
this.useSearch = (opts) => {
return useSearch({
select: opts?.select,
structuralSharing: opts?.structuralSharing,
from: this.options.id
});
};
this.useParams = (opts) => {
return useParams({
select: opts?.select,
structuralSharing: opts?.structuralSharing,
from: this.options.id
});
};
this.useLoaderDeps = (opts) => {
return useLoaderDeps({
...opts,
from: this.options.id
});
};
this.useLoaderData = (opts) => {
return useLoaderData({
...opts,
from: this.options.id
});
};
this.useNavigate = () => {
return useNavigate({ from: useRouter().routesById[this.options.id].fullPath });
};
this.options = opts;
}
};
/**
* Creates a lazily-configurable code-based route stub by ID.
*
* Use this for code-splitting with code-based routes. The returned function
* accepts only non-critical route options like `component`, `pendingComponent`,
* `errorComponent`, and `notFoundComponent` which are applied when the route
* is matched.
*
* @param id Route ID string literal to associate with the lazy route.
* @returns A function that accepts lazy route options and returns a `LazyRoute`.
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/createLazyRouteFunction
*/
function createLazyRoute(id) {
return (opts) => {
return new LazyRoute({
id,
...opts
});
};
}
/**
* Creates a lazily-configurable file-based route stub by file path.
*
* Use this for code-splitting with file-based routes (eg. `.lazy.tsx` files).
* The returned function accepts only non-critical route options like
* `component`, `pendingComponent`, `errorComponent`, and `notFoundComponent`.
*
* @param id File path literal for the route file.
* @returns A function that accepts lazy route options and returns a `LazyRoute`.
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/createLazyFileRouteFunction
*/
function createLazyFileRoute(id) {
if (typeof id === "object") return new LazyRoute(id);
return (opts) => new LazyRoute({
id,
...opts
});
}
//#endregion
export { FileRoute, FileRouteLoader, LazyRoute, createFileRoute, createLazyFileRoute, createLazyRoute };
//# sourceMappingURL=fileRoute.js.map