Hello Developers 👋,

Imagine changing the active tab in a dashboard and seeing the role badge render again. The badge still says editor, so what changed?

If your selector returns a fresh object, the object reference can change even when its contents don't. TanStack Router 1.171.0 made that behavior an explicitly typed option on useRouteContext.

What is structuralSharing?

useRouteContext is an existing hook that reads context from a route. The new structuralSharing option lets TanStack Router reuse unchanged parts of the value you select instead of handing React a fresh reference every time. If the whole selection is unchanged, it can reuse the whole object. The underlying mechanism already existed in the router. What's new is typed support on this hook.

Check out the release notes 🚀

How to use it?

Pass structuralSharing: true when your selector returns an object:

const { role } = Route.useRouteContext({ select: (context) => ({ role: context.role }), structuralSharing: true, }); 
  • select picks the data this component cares about.
  • structuralSharing lets the router keep the previous result when the selected values haven't changed.

When to use it?

Use it when you need multiple values in one selection and want to avoid rerenders driven only by a new object reference. If you're selecting just one string, select: (context) => context.role is usually simpler. Stick to plain data for structural sharing, not class instances or functions.

Example: Change the tab, not the badge

In a file-based TanStack Router app, add this as src/routes/dashboard.tsx:

import { createFileRoute, Link } from '@tanstack/react-router'; import { memo } from 'react'; type DashboardSearch = { tab: 'overview' | 'settings' }; export const Route = createFileRoute('/dashboard')({ validateSearch: (search): DashboardSearch => ({ tab: search.tab === 'settings' ? 'settings' : 'overview', }), beforeLoad: ({ search }) => ({ role: 'editor', tab: search.tab, }), component: Dashboard, }); const RoleBadge = memo(function RoleBadge() { const { role } = Route.useRouteContext({ select: (context) => ({ role: context.role }), structuralSharing: true, }); console.count('RoleBadge render'); return <p>Role: {role}</p>; }); function Dashboard() { const { tab } = Route.useSearch(); return ( <> <Link to="/dashboard" search={{ tab: tab === 'overview' ? 'settings' : 'overview' }} > Switch tab </Link> <p>Current tab: {tab}</p> <RoleBadge /> </> ); } 

Open /dashboard, clear the console after the first render, and switch tabs. Repeat with structuralSharing: false. Compare what happens on the click, not on initial mount. The badge is memoized so we're not just counting renders inherited from its parent.

That's it 😅

Same role doesn't always mean same object. This option helps with that specific problem; it doesn't stop every rerender.

Happy coding!