@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
27 lines (26 loc) • 1.15 kB
JavaScript
import { useRouterState } from "./useRouterState.js";
import { dummyMatchContext, matchContext } from "./matchContext.js";
import * as Solid from "solid-js";
import invariant from "tiny-invariant";
//#region src/useMatch.tsx
function useMatch(opts) {
const nearestMatchId = Solid.useContext(opts.from ? dummyMatchContext : matchContext);
const matchState = useRouterState({ select: (state) => {
const match = state.matches.find((d) => opts.from ? opts.from === d.routeId : d.id === nearestMatchId());
if (match === void 0) return {
match: void 0,
shouldThrowError: !state.pendingMatches?.find((d) => opts.from ? opts.from === d.routeId : d.id === nearestMatchId()) && !state.isTransitioning && (opts.shouldThrow ?? true)
};
return {
match: opts.select ? opts.select(match) : match,
shouldThrowError: false
};
} });
Solid.createEffect(() => {
if (matchState().shouldThrowError) invariant(false, `Could not find ${opts.from ? `an active match from "${opts.from}"` : "a nearest match!"}`);
});
return Solid.createMemo(() => matchState().match);
}
//#endregion
export { useMatch };
//# sourceMappingURL=useMatch.js.map