@tanstack/react-router
Version:
Modern and scalable routing for React applications
39 lines (38 loc) • 1.61 kB
JavaScript
require("./_virtual/_rolldown/runtime.cjs");
const require_useRouter = require("./useRouter.cjs");
let _tanstack_router_core = require("@tanstack/router-core");
let react = require("react");
let _tanstack_react_store = require("@tanstack/react-store");
let _tanstack_router_core_isServer = require("@tanstack/router-core/isServer");
//#region src/useLocation.tsx
/**
* Read the current location from the router state with optional selection.
* Useful for subscribing to just the pieces of location you care about.
*
* Options:
* - `select`: Project the `location` object to a derived value
* - `structuralSharing`: Enable structural sharing for stable references
*
* @returns The current location (or selected value).
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useLocationHook
*/
function useLocation(opts) {
const router = require_useRouter.useRouter();
if (_tanstack_router_core_isServer.isServer ?? router.isServer) {
const location = router.stores.location.state;
return opts?.select ? opts.select(location) : location;
}
const previousResult = (0, react.useRef)(void 0);
return (0, _tanstack_react_store.useStore)(router.stores.location, (location) => {
const selected = opts?.select ? opts.select(location) : location;
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.useLocation = useLocation;
//# sourceMappingURL=useLocation.cjs.map