-
-
Notifications
You must be signed in to change notification settings - Fork 649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cyclic dependency while importing a route object to a corresponding component module #2755
Comments
if you have the component defined in a another file, use the "global" hooks (e.g. |
@schiller-manuel Oh, I see, thank you. Unfortunately, I have another error while using the global // constants.ts
export const FOO_ROUTE_ID = 'foo'; // routes.ts
import { FOO_ROUTE_ID } from './constants.ts';
import { Foo } from './Foo.tsx';
export const fooRoute = createRoute({
id: FOO_ROUTE_ID,
component: Foo,
}); // Foo.tsx
import { FOO_ROUTE_ID } from './constants.ts';
export const Foo = () => {
// TS2345: Argument of type 'foo' is not assignable to parameter of type Constrain<'foo', '__root__' | '/foo'>
const { useRouteContext } = getRouteApi(FOO_ROUTE_ID);
// or
// TS2820: Type 'foo' is not assignable to type '__root__' | '/foo'. Did you mean '/foo'?
const ctx = useRouteContext({ from: FOO_ROUTE_ID });
...
}; |
looks like you would need to use |
https://stackblitz.com/edit/tanstack-router-hk6zmr?file=src%2Fmain.tsx It seems there is the same concatenating for constants as used for paths. For example, if |
@artptr the expected route ids are mentioned in the inferred types. |
@SeanCassiere Yes, but I would expect that it will be the same id the route was created with. Am I wrong about it? |
Yes, the inferred types will always reflect the actual route IDs that'd be present during runtime. Edit: |
Documentation says that id property is "optional, but required if a path is not provided". I can conclude it's optional if path is provided. Anyway, it should be possible to set an arbitrary id to any route to be able to use the route API as it seems to me. |
With regards to the documentation, we'd just need to update the verbiage in the documentation to make this distinctio of the runtime behaviour clearer. This restriction of not allowing both an router/packages/react-router/src/route.ts Lines 973 to 976 in 5e27e25
@schiller-manuel Your recent changes in the generator (#2597) made it so that the routeTree file used a |
Which project does this relate to?
Router
Describe the bug
Calling hooks from a route object causes a cyclic dependency issue. This problem arises because importing hooks into the route object requires importing the component itself. This import chain creates a circular dependency, where the component imports the route module to access the route object.
I assume the importance of type safety, but circular dependencies can also lead to test failures in Jest when attempting to mock components or functions involved in a cycle.
Your Example Website or App
Steps to Reproduce the Bug or Issue
Expected behavior
The component should be able to access route-dependent hooks without creating cyclic dependencies.
Screenshots or Videos
No response
Platform
Additional context
No response
The text was updated successfully, but these errors were encountered: