feat: provide location in onEnter/onStay/onLeave hooks #3622
main fromrefs/pull/3622/head·opened - 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-router
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: router-core
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: solid-router
- @@ -14,3 +14,4 @@1414 import './styles.css'15151616 const rootRoute = createRootRoute({17+ onEnter: (match, { location }) => {
All changes to this file can be removed, just makes testing easier
Open in Stackblitz
More templates
- tanstack-router-react-example-authenticated-routes
- tanstack-router-react-example-basic
- tanstack-router-react-example-basic-default-search-params
- tanstack-router-react-example-basic-file-based
- tanstack-router-react-example-react-query
- tanstack-router-react-example-basic-react-query-file-based
- tanstack-router-react-example-basic-ssr-file-based
- tanstack-router-react-example-basic-ssr-streaming-file-based
- tanstack-router-react-example-basic-virtual-file-based
- tanstack-router-react-example-basic-virtual-inside-file-based
- tanstack-router-react-example-deferred-data
- tanstack-router-react-example-kitchen-sink
- tanstack-router-react-example-kitchen-sink-file-based
- tanstack-router-react-example-kitchen-sink-react-query
- tanstack-router-react-example-kitchen-sink-react-query-file-based
- tanstack-router-react-example-large-file-based
- tanstack-router-react-example-location-masking
- tanstack-router-react-example-navigation-blocking
- tanstack-router-react-example-quickstart
- tanstack-router-react-example-quickstart-esbuild-file-based
- tanstack-router-react-example-quickstart-file-based
- tanstack-router-react-example-quickstart-rspack-file-based
- tanstack-router-react-example-quickstart-webpack-file-based
- router-monorepo-react-query
- router-mono-simple
- router-mono-simple-lazy
- tanstack-router-react-example-scroll-restoration
- tanstack-search-validator-adapters
- tanstack-start-example-bare
- tanstack-start-example-basic
- tanstack-start-example-basic-auth
- tanstack-start-example-basic-react-query
- tanstack-start-example-basic-rsc
- tanstack-start-example-basic-static
- tanstack-start-example-clerk-basic
- tanstack-start-example-convex-trellaux
- tanstack-start-example-counter
- tanstack-start-example-large
- tanstack-start-example-supabase-basic
- tanstack-start-example-trellaux
- tanstack-router-react-example-with-framer-motion
- tanstack-router-react-example-with-trpc
- tanstack-router-react-example-with-trpc-react-query
- tanstack-router-solid-example-basic-file-based
- tanstack-router-solid-example-basic-solid-query
- tanstack-router-solid-example-basic-solid-query-file-based
- tanstack-router-solid-example-kitchen-sink-file-based
- tanstack-router-solid-example-quickstart-file-based
- tanstack-solid-start-example-bare
@tanstack/arktype-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@3622@tanstack/create-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/create-router@3622@tanstack/create-start
npm i https://pkg.pr.new/TanStack/router/@tanstack/create-start@3622@tanstack/directive-functions-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/directive-functions-plugin@3622@tanstack/eslint-plugin-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@3622@tanstack/history
npm i https://pkg.pr.new/TanStack/router/@tanstack/history@3622@tanstack/react-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@3622@tanstack/react-router-with-query
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-with-query@3622@tanstack/react-start
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@3622@tanstack/react-start-api-routes
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-api-routes@3622@tanstack/react-start-client
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@3622@tanstack/react-start-config
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-config@3622@tanstack/react-start-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-plugin@3622@tanstack/react-start-router-manifest
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-router-manifest@3622@tanstack/react-start-server
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@3622@tanstack/react-start-server-functions-client
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server-functions-client@3622@tanstack/react-start-server-functions-fetcher
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server-functions-fetcher@3622@tanstack/react-start-server-functions-handler
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server-functions-handler@3622@tanstack/react-start-server-functions-ssr
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server-functions-ssr@3622@tanstack/router-cli
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@3622@tanstack/router-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@3622@tanstack/router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@3622@tanstack/router-generator
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@3622@tanstack/router-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@3622@tanstack/router-utils
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@3622@tanstack/router-vite-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@3622@tanstack/server-functions-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/server-functions-plugin@3622@tanstack/solid-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@3622@tanstack/solid-start
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@3622@tanstack/solid-start-api-routes
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-api-routes@3622@tanstack/solid-start-client
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@3622@tanstack/solid-start-config
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-config@3622@tanstack/solid-start-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-plugin@3622@tanstack/solid-start-router-manifest
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-router-manifest@3622@tanstack/solid-start-server
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@3622@tanstack/solid-start-server-functions-client
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server-functions-client@3622@tanstack/solid-start-server-functions-fetcher
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server-functions-fetcher@3622@tanstack/solid-start-server-functions-handler
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server-functions-handler@3622@tanstack/solid-start-server-functions-ssr
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server-functions-ssr@3622@tanstack/start
npm i https://pkg.pr.new/TanStack/router/@tanstack/start@3622@tanstack/start-config
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-config@3622@tanstack/start-server-functions-server
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-functions-server@3622@tanstack/valibot-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@3622@tanstack/virtual-file-routes
npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@3622@tanstack/zod-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@3622commit:
9d71302github-actions[bot](GitHub profile, opens in a new tab) added labelsdocumentation
- packages/solid-router/tests/route.test.tsx line 195@@ -191,3 +191,5 @@191191192192 expect(fn).toHaveBeenCalledWith({ foo: 'bar' })193193 })194+195+ it('should have location defined in router.load()', async () => {
rather
should have location defined in onEnter?please also add a test for onStay and onLeave
also applies to react-router of course
Was just copying the style from above
its in an onEnter describe block
- @@ -791,4 +791,4 @@791791 if (792792 typeof window !== 'undefined' &&793793 'CSS' in window &&794- typeof window.CSS?.supports === 'function'794+ typeof window.CSS.supports === 'function'
don't remove the
? - packages/router-core/src/route.ts line 868@@ -865,3 +865,4 @@865865 >,866866 TLoaderDeps867867 >,868+ { location }: { location: ParsedLocation<{}> },
we should be able to supply the search type here (same for the others as well)
probably makes sense to create a
type OnMatchOptions<TSearch> = {location:ParsedLocation<TSearch>and use this for all 3 methods?
i think this additional typing is a little out of my immediate grasp - can you give some more guidance
before we do that, what is the semantic of location in each of these hooks?
I think in case of onLeave location is anything BUT the match, so we cannot statically type the search here. whereas for the other hooks, the route's search is present in location
Its the location that will resolve (assuming nothing thrown) - played with current / next like the useBlocker stuff but decided on this approach.
schiller-manuel(GitHub profile, opens in a new tab)replied on
packages/solid-router/tests/route.test.tsxline 195also applies to react-router of course
jprosevear(GitHub profile, opens in a new tab)replied on
packages/solid-router/tests/route.test.tsxline 195Was just copying the style from above
i think this additional typing is a little out of my immediate grasp - can you give some more guidance
jprosevear(GitHub profile, opens in a new tab)replied on
packages/solid-router/tests/route.test.tsxline 195its in an onEnter describe block
schiller-manuel(GitHub profile, opens in a new tab)replied on
packages/router-core/src/route.tsline 868before we do that, what is the semantic of location in each of these hooks?
I think in case of onLeave location is anything BUT the match, so we cannot statically type the search here. whereas for the other hooks, the route's search is present in location
Its the location that will resolve (assuming nothing thrown) - played with current / next like the useBlocker stuff but decided on this approach.
@schiller-manuel how do you want to reconcile this?
Thanks for the PR. I'm closing it because it has fallen behind main (sorry it got lost in the shuffle), and the lifecycle implementation has since moved into router-core.
The feature is still useful, so we’ve opened #7778 to track a fresh implementation. A new PR based on that issue would be welcome if you are still interested!
Sign in to join this conversation.
View your CI Pipeline Execution ↗ for commit 406cec67449df42d1a256e69ee8e2c1848bec651.
nx affected --targets=test:eslint,test:unit,tes...nx run-many --target=build --exclude=examples/*...☁️ Nx Cloud last updated this comment at
2025-03-04 21:07:53UTC