ESLint

Require select on React TanStack Router state hooks

In @tanstack/react-router, the following hooks subscribe to router state via React's useSyncExternalStore:

  • useRouterState
  • useMatches
  • useChildMatches
  • useParentMatches

Calling useRouterState() without select subscribes to the entire router state. Calling useMatches() without select subscribes to the matches store, and useParentMatches() and useChildMatches() return slices of the matches array. Without select, the component can re-render whenever that subscribed router state or matches list changes, even if the component does not use the changed data. Passing a select function limits subscription-driven re-renders to changes in the selected value.

This rule applies only to @tanstack/react-router. It ignores hooks imported from @tanstack/solid-router, @tanstack/vue-router, or other modules, and is included in the flat/react-recommended and react-recommended configs.

See the Render Optimizations guide for more information.

Rule Details

Examples of incorrect code for this rule:

src/components/Header.tsx
/* eslint "@tanstack/router/no-unselected-router-state": "warn" */
import { useMatches, useRouterState } from '@tanstack/react-router'

export function Header() {
  const state = useRouterState()
  const matches = useMatches()

  return (
    <div>
      {state.location.pathname} ({matches.length})
    </div>
  )
}

Examples of correct code for this rule:

src/components/Header.tsx
/* eslint "@tanstack/router/no-unselected-router-state": "warn" */
import { useMatches, useRouterState } from '@tanstack/react-router'

export function Header() {
  const pathname = useRouterState({
    select: (state) => state.location.pathname,
  })
  const matchCount = useMatches({
    select: (matches) => matches.length,
  })

  return (
    <div>
      {pathname} ({matchCount})
    </div>
  )
}

Attributes

  • ✅ React Recommended (flat/react-recommended, react-recommended)
  • 🔧 Fixable