@tanstack/react-router
Version:
Modern and scalable routing for React applications
52 lines (51 loc) • 2.38 kB
JavaScript
const require_runtime = require("./_virtual/_rolldown/runtime.cjs");
const require_matchContext = require("./matchContext.cjs");
const require_useRouter = require("./useRouter.cjs");
let _tanstack_router_core = require("@tanstack/router-core");
let react = require("react");
react = require_runtime.__toESM(react);
let _tanstack_react_store = require("@tanstack/react-store");
let _tanstack_router_core_isServer = require("@tanstack/router-core/isServer");
//#region src/useMatch.tsx
var dummyStore = {
state: void 0,
get: () => void 0,
subscribe: () => () => {}
};
/**
* 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 = require_useRouter.useRouter();
const nearestMatchId = react.useContext(opts.from ? require_matchContext.dummyMatchContext : require_matchContext.matchContext);
const key = opts.from ?? nearestMatchId;
const matchStore = key ? opts.from ? router.stores.getMatchStoreByRouteId(key) : router.stores.activeMatchStoresById.get(key) : void 0;
if (_tanstack_router_core_isServer.isServer ?? router.isServer) {
const match = matchStore?.state;
if ((opts.shouldThrow ?? true) && !match) {
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!"}`);
(0, _tanstack_router_core.invariant)();
}
if (match === void 0) return;
return opts.select ? opts.select(match) : match;
}
const previousResult = react.useRef(void 0);
return (0, _tanstack_react_store.useStore)(matchStore ?? dummyStore, (match) => {
if ((opts.shouldThrow ?? true) && !match) {
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!"}`);
(0, _tanstack_router_core.invariant)();
}
if (match === void 0) return;
const selected = opts.select ? opts.select(match) : match;
if (opts.structuralSharing ?? router.options.defaultStructuralSharing) {
const shared = (0, _tanstack_router_core.replaceEqualDeep)(previousResult.current, selected);
previousResult.current = shared;
return shared;
}
return selected;
});
}
//#endregion
exports.useMatch = useMatch;
//# sourceMappingURL=useMatch.cjs.map