@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
30 lines (29 loc) • 1.43 kB
JavaScript
const require_runtime = require("./_virtual/_rolldown/runtime.cjs");
const require_useRouterState = require("./useRouterState.cjs");
const require_matchContext = require("./matchContext.cjs");
let solid_js = require("solid-js");
solid_js = require_runtime.__toESM(solid_js);
let tiny_invariant = require("tiny-invariant");
tiny_invariant = require_runtime.__toESM(tiny_invariant);
//#region src/useMatch.tsx
function useMatch(opts) {
const nearestMatchId = solid_js.useContext(opts.from ? require_matchContext.dummyMatchContext : require_matchContext.matchContext);
const matchState = require_useRouterState.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_js.createEffect(() => {
if (matchState().shouldThrowError) (0, tiny_invariant.default)(false, `Could not find ${opts.from ? `an active match from "${opts.from}"` : "a nearest match!"}`);
});
return solid_js.createMemo(() => matchState().match);
}
//#endregion
exports.useMatch = useMatch;
//# sourceMappingURL=useMatch.cjs.map