1
1
import React , { useContext , useMemo } from 'react'
2
2
import { Menu , Dropdown } from 'antd'
3
- import { Link , matchPath , PathPattern } from 'react-router-dom'
3
+ import { Link , matchPath , PathPattern , useLocation } from 'react-router-dom'
4
4
import {
5
5
MenuUnfoldOutlined ,
6
6
MenuFoldOutlined ,
@@ -26,11 +26,16 @@ const AppHeader: React.FC<React.PropsWithChildren<Props>> = (props) => {
26
26
const { render : renderLocaleSelector } = useLocaleSelector ( )
27
27
const themeCtxValue = useThemeCtx ( )
28
28
const {
29
- loadState : { routePath } ,
30
- resolvedLocale : { locale, localeKey } ,
29
+ resolvedLocale : { locale } ,
31
30
staticData,
32
31
} = themeCtxValue
33
32
33
+ // use location.pathname instead of loadState.routePath
34
+ // to calculate the active nav menu items
35
+ // because loadState.routePath may have param placeholder like /users/[uid]
36
+ // and it can't tell difference between /users/1 and /users/2
37
+ const { pathname } = useLocation ( )
38
+
34
39
const renderLogo = ( ( ) => {
35
40
const logoLink = ( ( ) => {
36
41
let result : string | undefined | null
@@ -73,7 +78,7 @@ const AppHeader: React.FC<React.PropsWithChildren<Props>> = (props) => {
73
78
const result = ( resolvedTopNavs ?? [ ] )
74
79
. map ( getActiveKeyIfMatch )
75
80
. filter ( Boolean )
76
- if ( ! result . includes ( routePath ) ) result . push ( routePath )
81
+ if ( ! result . includes ( pathname ) ) result . push ( pathname )
77
82
return result as string [ ]
78
83
79
84
function getActiveKeyIfMatch ( item : MenuConfig ) {
@@ -113,17 +118,15 @@ const AppHeader: React.FC<React.PropsWithChildren<Props>> = (props) => {
113
118
} else {
114
119
actualMatcher = matcher
115
120
}
116
- // use loadState.routePath instead of location.pathname
117
- // because location.pathname may contain trailing slash
118
- return ! ! matchPath ( actualMatcher , routePath )
121
+ return ! ! matchPath ( actualMatcher , pathname )
119
122
} else {
120
123
return matcher . some ( ( oneMatcher ) => {
121
- return ! ! matchPath ( oneMatcher , routePath )
124
+ return ! ! matchPath ( oneMatcher , pathname )
122
125
} )
123
126
}
124
127
}
125
128
}
126
- } , [ routePath , resolvedTopNavs ] )
129
+ } , [ pathname , resolvedTopNavs ] )
127
130
128
131
return (
129
132
< header className = { s . header } >
0 commit comments