Files
SnowLuma/.pnpm-store/v11/files/10/db82dbbbb87baa99d301b67feab2311dab4467766ac01aee4d687ce493ce2269c97d5bb0facab6f826ca2ee95f189408218e648451a6e4f24e59ee2be704ce
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

108 lines
2.8 KiB
Plaintext

import { useMatch } from './useMatch'
import type {
StructuralSharingOption,
ValidateSelected,
} from './structuralSharing'
import type {
AnyRouter,
RegisteredRouter,
ResolveUseParams,
StrictOrFrom,
ThrowConstraint,
ThrowOrOptional,
UseParamsResult,
} from '@tanstack/router-core'
export interface UseParamsBaseOptions<
TRouter extends AnyRouter,
TFrom,
TStrict extends boolean,
TThrow extends boolean,
TSelected,
TStructuralSharing,
> {
select?: (
params: ResolveUseParams<TRouter, TFrom, TStrict>,
) => ValidateSelected<TRouter, TSelected, TStructuralSharing>
shouldThrow?: TThrow
}
export type UseParamsOptions<
TRouter extends AnyRouter,
TFrom extends string | undefined,
TStrict extends boolean,
TThrow extends boolean,
TSelected,
TStructuralSharing,
> = StrictOrFrom<TRouter, TFrom, TStrict> &
UseParamsBaseOptions<
TRouter,
TFrom,
TStrict,
TThrow,
TSelected,
TStructuralSharing
> &
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>
export type UseParamsRoute<out TFrom> = <
TRouter extends AnyRouter = RegisteredRouter,
TSelected = unknown,
TStructuralSharing extends boolean = boolean,
>(
opts?: UseParamsBaseOptions<
TRouter,
TFrom,
/* TStrict */ true,
/* TThrow */ true,
TSelected,
TStructuralSharing
> &
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
) => UseParamsResult<TRouter, TFrom, true, TSelected>
/**
* Access the current route's path parameters with type-safety.
*
* Options:
* - `from`/`strict`: Specify the matched route and whether to enforce strict typing
* - `select`: Project the params object to a derived value for memoized renders
* - `structuralSharing`: Enable structural sharing for stable references
* - `shouldThrow`: Throw if the route is not found in strict contexts
*
* @returns The params object (or selected value) for the matched route.
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useParamsHook
*/
export function useParams<
TRouter extends AnyRouter = RegisteredRouter,
const TFrom extends string | undefined = undefined,
TStrict extends boolean = true,
TThrow extends boolean = true,
TSelected = unknown,
TStructuralSharing extends boolean = boolean,
>(
opts: UseParamsOptions<
TRouter,
TFrom,
TStrict,
ThrowConstraint<TStrict, TThrow>,
TSelected,
TStructuralSharing
>,
): ThrowOrOptional<
UseParamsResult<TRouter, TFrom, TStrict, TSelected>,
TThrow
> {
return useMatch({
from: opts.from!,
shouldThrow: opts.shouldThrow,
structuralSharing: opts.structuralSharing,
strict: opts.strict,
select: (match) => {
const params = opts.strict === false ? match.params : match._strictParams
return opts.select ? opts.select(params) : params
},
}) as any
}