diff --git a/.changeset/four-carrots-post.md b/.changeset/four-carrots-post.md new file mode 100644 index 0000000000..b9a1bcafee --- /dev/null +++ b/.changeset/four-carrots-post.md @@ -0,0 +1,5 @@ +--- +'@tanstack/react-router': patch +--- + +useMatches can skip useRef for structural sharing during SSR diff --git a/packages/react-router/src/Matches.tsx b/packages/react-router/src/Matches.tsx index 5036f6e727..fb935ab5be 100644 --- a/packages/react-router/src/Matches.tsx +++ b/packages/react-router/src/Matches.tsx @@ -234,10 +234,6 @@ export function useMatches< StructuralSharingOption, ): UseMatchesResult { const router = useRouter() - const previousResult = - React.useRef>( - undefined, - ) if (isServer ?? router.isServer) { const matches = router.stores.matches.get() as Array< @@ -249,6 +245,12 @@ export function useMatches< > } + const previousResult = + // eslint-disable-next-line react-hooks/rules-of-hooks + React.useRef>( + undefined, + ) + // eslint-disable-next-line react-hooks/rules-of-hooks return useStore(router.stores.matches, (matches) => { const selected = opts?.select