Skip to content
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

useRouteContext typing does not include context from beforeLoad #901

Closed
nick-kang opened this issue Dec 30, 2023 · 5 comments
Closed

useRouteContext typing does not include context from beforeLoad #901

nick-kang opened this issue Dec 30, 2023 · 5 comments

Comments

@nick-kang
Copy link

Describe the bug

// router.tsx
const dashboard = new Route({
  getParentRoute: () => root,
  path: 'dashboard',
  beforeLoad: () => {
    return { session: 'abc123' }
  },
  component: Dashboard,
})

// Dashboard.tsx
  const { session } = useRouteContext({  // session is not typed. session should be a string
    from: '/dashboard',
  })

Dashboard.tsx does not properly type the session from beforeLoad even though it is present in the route context.

Your Example Website or App

https://stackblitz.com/edit/github-73nmhk?file=src%2Fmain.tsx

Steps to Reproduce the Bug or Issue

See bug description

Expected behavior

useRouteContext should include the types from beforeLoad

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Chrome
  • Version: 1.0.5

Additional context

No response

@schiller-manuel
Copy link
Contributor

schiller-manuel commented Dec 31, 2023

You need to use as const:

const { session } = useRouteContext({ from: '/' as const }); 

see https://stackblitz.com/edit/github-73nmhk-cs1a8c?file=src%2Fmain.tsx

alternative:

const { session } = useRouteContext({ from: indexRoute.id  });

https://stackblitz.com/edit/github-73nmhk-q45wa2?file=src%2Fmain.tsx

@CHE1RON
Copy link
Contributor

CHE1RON commented Jan 2, 2024

@schiller-manuel Using the breadcrumbs example from the docs, how would you solve this issue there when retrieving routeContext from some match 🤔 (mainly asking because using/passing loader data to context does not seem possible). Migrating from react-router where I passed loader data to the handle prop

@schiller-manuel
Copy link
Contributor

do you have a complete example on e.g. stackblitz?

@tadeumaia
Copy link

@schiller-manuel
From our conversation on https://discord.com/channels/719702312431386674/1194007808010625136

context is not typed at the RootRoute beforeload, only their children.

@schiller-manuel
Copy link
Contributor

I'll close this as the original issue is resolved. Please feel free to open new issues for further discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants