UNPKG

@tanstack/react-router

Version:

Modern and scalable routing for React applications

53 lines (52 loc) 2.15 kB
"use client"; import { dummyMatchContext, matchContext } from "./matchContext.js"; import { useRouter } from "./useRouter.js"; import { invariant, replaceEqualDeep } from "@tanstack/router-core"; import * as React$1 from "react"; import { useStore } from "@tanstack/react-store"; import { isServer } from "@tanstack/router-core/isServer"; //#region src/useMatch.tsx var dummyStore = { get() {}, subscribe() { return { unsubscribe() {} }; } }; function useStructuralSharing(opts, router) { const previousResult = React$1.useRef(); return (slice) => { const selected = opts?.select ? opts.select(slice) : slice; if (opts?.structuralSharing ?? router.options.defaultStructuralSharing) return previousResult.current = replaceEqualDeep(previousResult.current, selected); return selected; }; } /** * Read and select the nearest or targeted route match. * @link https://tanstack.com/router/latest/docs/framework/react/api/router/useMatchHook */ function useMatch(opts) { const router = useRouter(); const nearestMatchId = React$1.useContext(opts.from ? dummyMatchContext : matchContext); const matchStore = opts.from ? router.stores.getRouteMatchStore(opts.from) : router.stores.matchStores.get(nearestMatchId); if (isServer ?? router.isServer) { const match = matchStore?.get(); if (!match) { if (opts.shouldThrow ?? true) { if (process.env.NODE_ENV !== "production") throw new Error(`Invariant failed: Could not find ${opts.from ? `an active match from "${opts.from}"` : "a nearest match!"}`); invariant(); } return; } return opts.select ? opts.select(match) : match; } const selector = useStructuralSharing(opts, router); const matchSelection = useStore(matchStore ?? dummyStore, (match) => match ? selector(match) : dummyStore); if (matchSelection !== dummyStore) return matchSelection; if (opts.shouldThrow ?? true) { if (process.env.NODE_ENV !== "production") throw new Error(`Invariant failed: Could not find ${opts.from ? `an active match from "${opts.from}"` : "a nearest match!"}`); invariant(); } } //#endregion export { useMatch, useStructuralSharing }; //# sourceMappingURL=useMatch.js.map