Params type error
I'm trying to get an individual post on url:
if I'm using code below it navigates to correct route but no data is loaded on page and show's in dev tools but if I add route doesn't appear in dev tools and Link throws type error for params.
Router Config
"/blog/$postslug"
"/blog/$postslug"
"blog/$postSlug"
"blog/$postSlug"
export const postRoute = blogRoute.createRoute({
path: "$postSlug",
onLoad: async ({ params: { postSlug } }) => postLoader.preload(postSlug),
component: Post,
});
export const postRoute = blogRoute.createRoute({
path: "$postSlug",
onLoad: async ({ params: { postSlug } }) => postLoader.preload(postSlug),
component: Post,
});
import {
Loader,
LoaderClient,
LoaderClientProvider,
} from "@tanstack/react-loaders";
import { ReactRouter, RouterProvider } from "@tanstack/react-router"
import { indexRoute } from "./routes";
import { aboutRoute } from "./routes/about";
import { blogRoute } from "./routes/blog";
import { postRoute } from "./routes/post";
import { rootRoute } from "./routes/__root";
import { trpcVanilla } from "./utils/trpc";
export const postsLoader = new Loader({
key: "posts",
loader: async () => {
return await trpcVanilla.contentful.getAllPosts.query({});
},
});
export const postLoader = postsLoader.createLoader({
key: "post",
loader: async (postSlug: string) => {
return await trpcVanilla.contentful.getPost.query({
slug: postSlug
});
},
});
export const loaderClient = new LoaderClient({
loaders: [postsLoader, postLoader],
defaultGcMaxAge: 3600,
});
const routeConfig = rootRoute.addChildren([
indexRoute,
aboutRoute,
blogRoute.addChildren([postRoute]),
]);
export const router = new ReactRouter({
routeConfig,
});
export function Router() {
return (
<>
<LoaderClientProvider loaderClient={loaderClient}>
<RouterProvider router={router} defaultPreload="intent" />
</LoaderClientProvider>
</>
);
}
declare module "@tanstack/react-router" {
interface RegisterRouter {
router: typeof router;
}
}
declare module "@tanstack/react-loaders" {
interface RegisterLoaderClient {
loaderClient: typeof loaderClient;
}
}
import {
Loader,
LoaderClient,
LoaderClientProvider,
} from "@tanstack/react-loaders";
import { ReactRouter, RouterProvider } from "@tanstack/react-router"
import { indexRoute } from "./routes";
import { aboutRoute } from "./routes/about";
import { blogRoute } from "./routes/blog";
import { postRoute } from "./routes/post";
import { rootRoute } from "./routes/__root";
import { trpcVanilla } from "./utils/trpc";
export const postsLoader = new Loader({
key: "posts",
loader: async () => {
return await trpcVanilla.contentful.getAllPosts.query({});
},
});
export const postLoader = postsLoader.createLoader({
key: "post",
loader: async (postSlug: string) => {
return await trpcVanilla.contentful.getPost.query({
slug: postSlug
});
},
});
export const loaderClient = new LoaderClient({
loaders: [postsLoader, postLoader],
defaultGcMaxAge: 3600,
});
const routeConfig = rootRoute.addChildren([
indexRoute,
aboutRoute,
blogRoute.addChildren([postRoute]),
]);
export const router = new ReactRouter({
routeConfig,
});
export function Router() {
return (
<>
<LoaderClientProvider loaderClient={loaderClient}>
<RouterProvider router={router} defaultPreload="intent" />
</LoaderClientProvider>
</>
);
}
declare module "@tanstack/react-router" {
interface RegisterRouter {
router: typeof router;
}
}
declare module "@tanstack/react-loaders" {
interface RegisterLoaderClient {
loaderClient: typeof loaderClient;
}
}

0 Replies