Skip to content
Source
Sign inSign up
TanStack/router

`Route.useRouteContext()` returns an empty object instead of null #7110

OpenCarelessInternet(GitHub profile, opens in a new tab) opened this issue 0 comments

Which project does this relate to?

Router

Describe the bug

As the title says, the Route.useRouteContext() function returns an empty object instead of null when defining beforeLoad to return null, regardless if it's a server function or not. The same behaviour does not occur with the loader and Route.useLoaderData functions. The type for Route.useRouteContext is correct however.

Your Example Website or App

https://codesandbox.io/p/devbox/2lylk3

Steps to Reproduce the Bug or Issue

  1. Go to the reproduction link.
index.tsx:
import { createFileRoute } from "@tanstack/react-router";
import { createServerFn } from "@tanstack/react-start";

export const foo = createServerFn({ method: "GET" }).handler(() => null);

export const Route = createFileRoute("/")({
  beforeLoad: async () => await foo(),
  component: Home,
  loader: async () => await foo(),
});

function Home() {
  const state1 = Route.useRouteContext();
  const state2 = Route.useLoaderData();
  console.log("state1", state1, !!state1);
  console.log("state2", state2, !!state2);

  return <p>Hi</p>;
}
  1. Check the terminal output.
state1 {} true
state2 null false
state1 {} true
state2 null false

Expected behavior

Route.useRouteContext should return null.

Screenshots or Videos

No response

Platform

  • Router / Start Version: 1.168.10 / 1.167.16
  • OS: Linux
  • Browser: Firefox (Zen)
  • Browser Version: 1.19.5b (Zen)
  • Bundler: Vite
  • Bundler Version: 8.0.5

Additional context

I am using TanStack Start but that shouldn't matter.

Sign in to join this conversation.