Error: Maximum update depth exceeded while using Route.useLoaderData()
I have this server function.
loader: async () => {
return {
project: getProjectFn(),
};
}
Then I get error once I use const { project } = Route.useLoaderData(); in the component. Should I not use server function in loader?
8 Replies
probable-pinkOP•9mo ago
Here is a small repo. https://github.com/webmasterdevlin/tanstack-start-problems
GitHub
GitHub - webmasterdevlin/tanstack-start-problems
Contribute to webmasterdevlin/tanstack-start-problems development by creating an account on GitHub.
probable-pinkOP•9mo ago
Switch this to see the problem.
xenial-black•9mo ago
Neither technically should work (I believe). Server functions can only return serializable info and
Date
is not. So return Date().toISOString()
in the server function and then create your date object on the front end for display from the ISO String response.
https://tanstack.com/router/latest/docs/framework/react/start/server-functions#returning-primitives-and-jsonServer Functions | TanStack Router React Docs
What are Server Functions? Server functions allow you to specify logic that can be invoked almost anywhere (even the client), but run only on the server. In fact, they are not so different from an API...
xenial-black•9mo ago
Example Component:
And serverFn:
adverse-sapphire•9mo ago
no, I'm pretty sure TSS includes special support for Date serialisation
worth trying anyway but I'm pretty sure that's not it
probable-pinkOP•9mo ago
I added the toISOString(), still does not work. It used to be Prisma ORM. I hardcoded the value now so anyone can try it without using Prisma.
Even just returng an ID wont work.
xenial-black•9mo ago
Did you copy paste it as is? I just tried my changes locally with your code and it worked.
Could be, I just know
Date
isn't serializable by default.
And their example in the docs showed them using the toISOString
method as wellprobable-pinkOP•9mo ago
It is working in this code for getting data. But not if you switch from getProjectFn to Route.useLoaderData();
Anyone here is also getting this error?
I edited my previous response here.
I guess putting the getProjectFn in the loader of my __root won't really work. I moved it to const project = useServerFn(getProjectFn); instead