#useSearch paths includes layout routes

20 messages · Page 1 of 1 (latest)

stiff gorge
#

I have set up Code based routing where I have a couple of layout routes (using id instead of path).
When I try to use the hook useSearch the provided paths have the layout id prefixed. The useNavigateHook does not do this and works perfectly.

for example i want to access my index route this is what I want to do:
const filters = useSearch({ from: ''/weather" })
but I get a type error and it actually wants this:
const filters = useSearch({ from: '/_dashboardLayout/weather' })

The supplied picture includes my route setup or down below

`const dashboardLayoutRoute = createRoute({
getParentRoute: () => rootRoute,
id: '_dashboardLayout',
component: () => {
return (
<AppLayout
siteMenu={<SiteMenu />}
userMenu={<UserMenu />}
menuItems={getCustomerMenuItems()}
menuItemsPostAdornments={(drawerOpen) => (
<>
<HelpCentreMenuItem drawerOpen={drawerOpen} />
<FeedbackMenuItem drawerOpen={drawerOpen} />
</>
)}>
<Outlet />
<SpeedDialComponent />
</AppLayout>
)
}
})

const indexRoute = createRoute({
getParentRoute: () => dashboardLayoutRoute,
path: PageRoutes.DASHBOARD,
component: DashboardPage,
validateSearch: (search: Record<string, unknown>): FiltersStore => {
return {
dateFrom: search.dateFrom
? new Date(String(search.dateFrom))
: add(extractDateWithoutTime(new Date()), { months: -1 }),
dateTo: search.dateTo
? new Date(String(search.dateTo))
: add(new Date(), { hours: 1 }),
selectedDeviceIds: search.selectedDeviceIds
? (search.selectedDeviceIds as number[])
: undefined,
showTrafficLights:
localStorage.getItem(TRAFFIC_LIGHT_SETTING) === null
? true
: localStorage.getItem(TRAFFIC_LIGHT_SETTING) === 'true'
}
}
})
const routeTree = rootRoute.addChildren([
indexRoute,
])`

scarlet delta
#

When you use the second option, it works yes?

#

Generally, most of the hooks use the routeId instead of the path.

stiff gorge
#

No it doesn't work.

#

If I use useNavigate it provides the correct paths:

scarlet delta
#

Could you please throw this into a stackblitz env.

#

It'll be easier to debug what's going on.

stiff gorge
#

For sure

scarlet delta
#

One of the many reasons why we don't recommend code-based 😅
To get the kinds of type-safety being offered here, your configuration NEEDS to be verbose and done correctly.

#

If you really don't want to use TSR file-based routing conventions, you could possible looks at the virtual file routes
https://tanstack.com/router/latest/docs/framework/react/guide/virtual-file-routes

Its our version of what Remix/React-Router is doing with their "routes.ts" file, so you don't need to use our file-based routing conventions, you can use your own.

Headless, type-safe, powerful utilities for complex workflows like Data Management, Data Visualization, Charts, Tables, and UI Components.

stiff gorge
#

Sorry I'm trying to find the difference in what you did, I must be blind 😆

scarlet delta
#

Scroll down to the routeTree

#
const routeTree = ...
stiff gorge
#

ah I see haha

#

Yea interesting, is there a reason why useNavigate and useSearch have different paths?

scarlet delta
#

Its mostly for ts-perf, some hooks need the routeId whilst others need the routePath

stiff gorge
#

Ah okay