Skip to content

Since version 1.28.2, the useRouteContext() hook returns undefined at the first start #1478

@wuarmin

Description

@wuarmin

Describe the bug

It seems that in release 1.28.2 the behavior of useRouteContext or beforeLoad has changed.

I return a context-change from beforeLoad, but the useRouteContext-hook return undefined in the component.

export const Route = createFileRoute('/')({
  beforeLoad: () => ({ user: 'Armin' }),
  component: HomeComponent,
});

function HomeComponent() {
  console.log('render HomeComponent');
  const ctx = Route.useRouteContext(); // ctx is undefined
  console.log(ctx);
  return (
    <div className="p-2">
      <h3>Welcome Home! {ctx.user}</h3> // this fails!!! if I change it to ctx?.user the user-name appears, but only after joining the route again
    </div>
  );
}

Your Example Website or App

https://stackblitz.com/edit/tanstack-router-cfgel4?file=src%2Froutes%2Findex.tsx

Steps to Reproduce the Bug or Issue

If you go to Home, you'll see an error:

can't access property "user", ctx is undefined

If I change the code to ctx?.user, the context is returned and the user-name appears, but only after joining the route again.

Expected behavior

The context should not be undefined.

Screenshots or Videos

No response

Platform

  • OS: Linux
  • Browser: Firefox
  • Version: 126.0b1

Additional context

Question in discord: https://discord.com/channels/719702312431386674/1230086872672239627

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions