UNPKG

@tanstack/solid-router

Version:

Modern and scalable routing for Solid applications

39 lines (38 loc) 1.49 kB
import { useRouter } from "./useRouter.js"; import { nearestMatchContext } from "./matchContext.js"; import { invariant, replaceEqualDeep } from "@tanstack/router-core"; import * as Solid from "solid-js"; //#region src/useMatch.tsx function useMatch(opts) { const router = useRouter(); const nearestMatch = opts.from ? void 0 : Solid.useContext(nearestMatchContext); const match = () => { if (opts.from) { const ids = router.stores.matchesId.state; for (const id of ids) { const matchStore = router.stores.activeMatchStoresById.get(id); if (matchStore?.routeId === opts.from) return matchStore.state; } return; } return nearestMatch?.match(); }; return Solid.createMemo((prev) => { const selectedMatch = match(); if (selectedMatch === void 0) { const hasPendingMatch = opts.from ? Boolean(router.stores.pendingRouteIds.state[opts.from]) : nearestMatch?.hasPending() ?? false; const isTransitioning = router.stores.isTransitioning.state; if (!hasPendingMatch && !isTransitioning && (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; } const res = opts.select ? opts.select(selectedMatch) : selectedMatch; if (prev === void 0) return res; return replaceEqualDeep(prev, res); }); } //#endregion export { useMatch }; //# sourceMappingURL=useMatch.js.map