Skip to content

Commit 29dfcf9

Browse files
committed
refactor based on coderabbit recommendations
1 parent 87d51b9 commit 29dfcf9

3 files changed

Lines changed: 41 additions & 54 deletions

File tree

packages/react-router/src/link.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,13 @@ export function useLinkProps<
9999
structuralSharing: true as any,
100100
})
101101

102-
const { getFromPath, activeLocation } = useActiveLocation()
102+
// subscribe to location here to re-build fromPath if it changes
103+
const routerLocation = useRouterState({
104+
select: (s) => s.location,
105+
structuralSharing: true as any,
106+
})
107+
108+
const { getFromPath } = useActiveLocation()
103109

104110
const from = getFromPath(options.from)
105111

@@ -110,7 +116,7 @@ export function useLinkProps<
110116
// eslint-disable-next-line react-hooks/exhaustive-deps
111117
[
112118
router,
113-
activeLocation,
119+
routerLocation,
114120
currentSearch,
115121
from,
116122
options._fromLocation,

packages/react-router/src/useActiveLocation.ts

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,25 @@ import { last } from '@tanstack/router-core'
22
import { useCallback, useEffect, useState } from 'react'
33
import { useRouter } from './useRouter'
44
import { useMatch } from './useMatch'
5-
import type { AnyRouteMatch, ParsedLocation } from '@tanstack/router-core'
5+
import { useRouterState } from './useRouterState'
6+
import type { ParsedLocation } from '@tanstack/router-core'
67

7-
export type UseLocationResult = {
8-
activeLocationMatch: AnyRouteMatch | undefined
8+
export type UseActiveLocationResult = {
9+
activeLocation: ParsedLocation
910
getFromPath: (from?: string) => string
11+
setActiveLocation: (location?: ParsedLocation) => void
1012
}
1113

12-
export const useActiveLocation = (location?: ParsedLocation) => {
13-
const { matchRoutes, state } = useRouter()
14-
const [activeLocation, setActiveLocation] = useState<ParsedLocation>(
15-
location ?? state.location,
16-
)
17-
const [customActiveLocation, _setCustomActiveLocation] =
18-
useState<ParsedLocation>(location ?? state.location)
19-
const [useCustomActiveLocation, setUseCustomActiveLocation] =
20-
useState(!!location)
14+
export const useActiveLocation = (location?: ParsedLocation): UseActiveLocationResult => {
15+
const router = useRouter()
16+
const routerLocation = useRouterState({select: (state) => state.location})
17+
const [activeLocation, setActiveLocation] = useState<ParsedLocation>(location ?? routerLocation)
18+
const [customActiveLocation, setCustomActiveLocation] = useState<ParsedLocation | undefined>(location)
2119

2220
useEffect(() => {
23-
if (!useCustomActiveLocation) {
24-
setActiveLocation(state.location)
25-
} else {
26-
setActiveLocation(customActiveLocation)
27-
}
28-
}, [state.location, useCustomActiveLocation, customActiveLocation])
29-
30-
const setCustomActiveLocation = (location: ParsedLocation) => {
31-
_setCustomActiveLocation(location)
32-
setUseCustomActiveLocation(true)
33-
}
21+
setActiveLocation(customActiveLocation ?? routerLocation)
22+
}, [routerLocation, customActiveLocation])
23+
3424

3525
const currentRouteMatch = useMatch({
3626
strict: false,
@@ -39,15 +29,15 @@ export const useActiveLocation = (location?: ParsedLocation) => {
3929

4030
const getFromPath = useCallback(
4131
(from?: string) => {
42-
const activeLocationMatches = matchRoutes(activeLocation, {
32+
const activeLocationMatches = router.matchRoutes(activeLocation, {
4333
_buildLocation: false,
4434
})
4535

4636
const activeLocationMatch = last(activeLocationMatches)
4737

4838
return from ?? activeLocationMatch?.fullPath ?? currentRouteMatch.fullPath
4939
},
50-
[activeLocation, currentRouteMatch.fullPath, matchRoutes],
40+
[activeLocation, currentRouteMatch.fullPath, router],
5141
)
5242

5343
return {

packages/solid-router/src/useActiveLocation.ts

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,42 @@
11
import { last } from '@tanstack/router-core'
2-
import { createEffect, createSignal } from 'solid-js'
3-
import * as Solid from 'solid-js'
2+
import { createEffect, createMemo, createSignal } from 'solid-js'
43
import { useMatch } from './useMatch'
54
import { useRouter } from './useRouter'
65
import { useRouterState } from './useRouterState'
6+
import type {Accessor} from 'solid-js';
77
import type { ParsedLocation } from '@tanstack/router-core'
88

9-
export function useActiveLocation(location?: ParsedLocation) {
9+
export type UseLocationResult = {
10+
activeLocation: Accessor<ParsedLocation>
11+
getFromPath: (from?: string) => Accessor<string>
12+
setActiveLocation: (location?: ParsedLocation) => void
13+
}
14+
15+
export function useActiveLocation(location?: ParsedLocation): UseLocationResult {
1016
const router = useRouter()
11-
const [activeLocation, setActiveLocation] = createSignal<ParsedLocation>(
12-
location ?? useRouterState({ select: (state) => state.location })(),
13-
)
14-
const [customActiveLocation, _setCustomActiveLocation] =
15-
createSignal<ParsedLocation>(
16-
location ?? useRouterState({ select: (state) => state.location })(),
17-
)
18-
const [useCustomActiveLocation, setUseCustomActiveLocation] =
19-
createSignal(!!location)
17+
// we are not using a variable here for router state location since we need to only calculate that if the location is not passed in. It can result in unnecessary history actions if we do that.
18+
const [activeLocation, setActiveLocation] = createSignal<ParsedLocation>(location ?? useRouterState({select: s => s.location})())
19+
const [customActiveLocation, setCustomActiveLocation] = createSignal<ParsedLocation | undefined>(location)
2020

2121
createEffect(() => {
22-
if (!useCustomActiveLocation()) {
23-
setActiveLocation(useRouterState({ select: (state) => state.location }))
24-
} else {
25-
setActiveLocation(customActiveLocation())
26-
}
22+
setActiveLocation(customActiveLocation() ?? useRouterState({select: s => s.location})())
2723
})
2824

29-
const setCustomActiveLocation = (location: ParsedLocation) => {
30-
_setCustomActiveLocation(location)
31-
setUseCustomActiveLocation(true)
32-
}
33-
34-
const matchIndex = useMatch({
25+
const currentRouteMatch = useMatch({
3526
strict: false,
36-
select: (match) => match.index,
27+
select: (match) => match,
3728
})
3829

3930
const getFromPath = (from?: string) =>
40-
Solid.createMemo(() => {
41-
const currentRouteMatches = router.matchRoutes(activeLocation(), {
31+
createMemo(() => {
32+
const currentRouteMatches = router.matchRoutes(customActiveLocation() ?? activeLocation(), {
4233
_buildLocation: false,
4334
})
4435

4536
return (
4637
from ??
4738
last(currentRouteMatches)?.fullPath ??
48-
router.state.matches[matchIndex()]!.fullPath
39+
currentRouteMatch().fullPath
4940
)
5041
})
5142

0 commit comments

Comments
 (0)