Skip to content

Commit 4a93cff

Browse files
authored
refactor(react-router): useMatches skips useRef during SSR (#7575)
1 parent 757d433 commit 4a93cff

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

.changeset/four-carrots-post.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/react-router': patch
3+
---
4+
5+
useMatches can skip useRef for structural sharing during SSR

packages/react-router/src/Matches.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,6 @@ export function useMatches<
234234
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
235235
): UseMatchesResult<TRouter, TSelected> {
236236
const router = useRouter<TRouter>()
237-
const previousResult =
238-
React.useRef<ValidateSelected<TRouter, TSelected, TStructuralSharing>>(
239-
undefined,
240-
)
241237

242238
if (isServer ?? router.isServer) {
243239
const matches = router.stores.matches.get() as Array<
@@ -249,6 +245,12 @@ export function useMatches<
249245
>
250246
}
251247

248+
const previousResult =
249+
// eslint-disable-next-line react-hooks/rules-of-hooks
250+
React.useRef<ValidateSelected<TRouter, TSelected, TStructuralSharing>>(
251+
undefined,
252+
)
253+
252254
// eslint-disable-next-line react-hooks/rules-of-hooks
253255
return useStore(router.stores.matches, (matches) => {
254256
const selected = opts?.select

0 commit comments

Comments
 (0)