Files
SnowLuma/.pnpm-store/v11/files/be/24513813ee28f58acc52f5ebd9bfd76058768dec7b04579ea2d4ac7c842881e2389378d6f0bc387c7402f9fbab5b4391a0c9f372b0578dc119fb82f8cdd225
TanTan 2a69b23b47
Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Skipped
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

82 lines
2.6 KiB
Plaintext

'use client'
import { useStore } from '@tanstack/react-store'
import { useRef } from 'react'
import { replaceEqualDeep } from '@tanstack/router-core'
import { isServer } from '@tanstack/router-core/isServer'
import { useRouter } from './useRouter'
import type {
StructuralSharingOption,
ValidateSelected,
} from './structuralSharing'
import type {
AnyRouter,
RegisteredRouter,
RouterState,
} from '@tanstack/router-core'
export interface UseLocationBaseOptions<
TRouter extends AnyRouter,
TSelected,
TStructuralSharing extends boolean = boolean,
> {
select?: (
state: RouterState<TRouter['routeTree']>['location'],
) => ValidateSelected<TRouter, TSelected, TStructuralSharing>
}
export type UseLocationResult<
TRouter extends AnyRouter,
TSelected,
> = unknown extends TSelected
? RouterState<TRouter['routeTree']>['location']
: TSelected
/**
* Read the current location from the router state with optional selection.
* Useful for subscribing to just the pieces of location you care about.
*
* Options:
* - `select`: Project the `location` object to a derived value
* - `structuralSharing`: Enable structural sharing for stable references
*
* @returns The current location (or selected value).
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useLocationHook
*/
export function useLocation<
TRouter extends AnyRouter = RegisteredRouter,
TSelected = unknown,
TStructuralSharing extends boolean = boolean,
>(
opts?: UseLocationBaseOptions<TRouter, TSelected, TStructuralSharing> &
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
): UseLocationResult<TRouter, TSelected> {
const router = useRouter<TRouter>()
if (isServer ?? router.isServer) {
const location = router.stores.location.get()
return (
opts?.select ? opts.select(location as any) : location
) as UseLocationResult<TRouter, TSelected>
}
const previousResult =
// eslint-disable-next-line react-hooks/rules-of-hooks -- condition is static
useRef<ValidateSelected<TRouter, TSelected, TStructuralSharing>>(undefined)
// eslint-disable-next-line react-hooks/rules-of-hooks -- condition is static
return useStore(router.stores.location, (location) => {
const selected = (
opts?.select ? opts.select(location as any) : location
) as ValidateSelected<TRouter, TSelected, TStructuralSharing>
if (opts?.structuralSharing ?? router.options.defaultStructuralSharing) {
const shared = replaceEqualDeep(previousResult.current, selected)
previousResult.current = shared
return shared
}
return selected
}) as UseLocationResult<TRouter, TSelected>
}