In @tanstack/react-router, hooks that accept a select function compare the previous and next selected values by reference equality by default. When select returns a new object or array literal (for example select: (s) => ({ page: s.page, tab: s.tab }) or select: (matches) => matches.map((m) => m.routeId)), React sees a new reference on every router state update and re-renders the component even when all nested values are unchanged.
Enabling structuralSharing: true on the hook call (or setting defaultStructuralSharing: true globally in createRouter) compares JSON-compatible objects and arrays deeply and preserves previous references when their contents are equal.
This type-aware rule applies only to @tanstack/react-router hooks that support structuralSharing:
The rule inspects TypeScript type information at each callsite and only reports when all of the following hold:
The rule intentionally ignores:
This rule requires type-aware linting (parserOptions.projectService: true) and is included in the flat/react-recommended-type-checked and react-recommended-type-checked configs. Files without TypeScript type information (for example JavaScript files or files covered by disableTypeChecked) are skipped automatically.
See the Render Optimizations guide for more information.
Examples of incorrect code for this rule (when defaultStructuralSharing is not true on the router):
/* eslint "@tanstack/router/prefer-structural-sharing": "warn" */
import { useMatches, useSearch } from '@tanstack/react-router'
export function PostFilters() {
const filters = useSearch({
from: '/posts',
select: (search) => ({
page: search.page,
tag: search.tag,
}),
})
const routeIds = useMatches({
select: (matches) => matches.map((match) => match.routeId),
})
return (
<div>
{filters.page} - {routeIds.join('/')}
</div>
)
}Examples of correct code for this rule:
/* eslint "@tanstack/router/prefer-structural-sharing": "warn" */
import { useLoaderData, useMatches, useSearch } from '@tanstack/react-router'
export function PostFilters() {
// Enable structuralSharing on the hook call
const filters = useSearch({
from: '/posts',
select: (search) => ({
page: search.page,
tag: search.tag,
}),
structuralSharing: true,
})
// Explicit opt-out is respected
const routeIds = useMatches({
select: (matches) => matches.map((match) => match.routeId),
structuralSharing: false,
})
// Primitive selectors already compare equal by reference
const page = useSearch({
from: '/posts',
select: (search) => search.page,
})
// Non-JSON values (such as Date or functions) are not flagged
const createdAt = useLoaderData({
from: '/posts',
select: (data) => data.createdAt,
})
return (
<div>
{page} - {filters.tag} - {routeIds.length} - {createdAt.toISOString()}
</div>
)
}import { createRouter } from '@tanstack/react-router'
import { routeTree } from './routeTree.gen'
// Or enable defaultStructuralSharing globally on your registered router:
export const router = createRouter({
routeTree,
defaultStructuralSharing: true,
})
declare module '@tanstack/react-router' {
interface Register {
router: typeof router
}
}