Loaders deps less verbose #2505
-
Is there a way to make the loaderDeps functionality a bit less verbose? export const Route = createFileRoute('/_app/claims/')({
validateSearch: zodSearchValidator(ClaimsRequest),
loaderDeps: ({ search: { pageNumber,
pageSize,
search,
column,
desc,
eventTypes,
registers,
destuctionDegrees } }) => ({
pageNumber,
pageSize,
search,
column,
desc,
eventTypes,
registers,
destuctionDegrees
}),
loader: ({ deps: {
pageNumber,
pageSize,
search,
column,
desc,
eventTypes,
registers,
destuctionDegrees
} }) => queryClient.ensureQueryData(claimsQueries.get({
filters: {
pageNumber,
pageSize,
search,
column,
desc,
eventTypes,
registers,
destuctionDegrees
}
})),
component: () => Claims()
}) I can simplify this by a little by not destructuring the deps validateSearch: zodSearchValidator(ClaimsRequest),
loaderDeps: ({ search: { pageNumber,
pageSize,
search,
column,
desc,
eventTypes,
registers,
destuctionDegrees } }) => ({
pageNumber,
pageSize,
search,
column,
desc,
eventTypes,
registers,
destuctionDegrees
}),
loader: ({ deps }) => queryClient.ensureQueryData(claimsQueries.get({ filters: deps })), But this will result in unnecessary deps in the query being pushed into the api like I really like the loader in combination with the validateSearch and loaderDeps i just wish there is a bit of a cleaner way to do this. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
we have a function you could use it like this: export function pick<TValue, TKey extends keyof TValue>(
parent: TValue,
keys: Array<TKey>,
): Pick<TValue, TKey> {
return keys.reduce((obj: any, key: TKey) => {
obj[key] = parent[key]
return obj
}, {} as any)
}
export const Route = createFileRoute('/_app/claims/')({
validateSearch: zodSearchValidator(ClaimsRequest),
loaderDeps: ({ search }) => pick(search, [
'pageNumber',
'pageSize',
'search',
'column',
'desc',
'eventTypes',
'registers',
'destuctionDegrees'
]),
loader: ({ deps }) => queryClient.ensureQueryData(claimsQueries.get({
filters: deps
})),
component: () => Claims()
}) I don't get the part about "But this will result in unnecessary deps in the query being pushed into the api like &test=1 will be included into the call" |
Beta Was this translation helpful? Give feedback.
we have a function
pick
in router, although it's exported it's not part of the public API.you could use it like this: