Skip to content

Commit 394ce4f

Browse files
dongruisunnywx
authored andcommitted
fix: Use locaion in componentDidMount (#75)
1 parent f8b47d4 commit 394ce4f

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/components/Header/index.jsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default class Header extends React.Component {
2929
}
3030

3131
state = {
32+
currentPath: '/',
3233
showMenus: false,
3334
isDarkBg: false,
3435
}
@@ -39,6 +40,8 @@ export default class Header extends React.Component {
3940
if (maxTop > 0) {
4041
window.onscroll = this.handleScroll
4142
}
43+
44+
this.setState({ currentPath: location.pathname })
4245
}
4346

4447
componentWillUnmount() {
@@ -64,6 +67,7 @@ export default class Header extends React.Component {
6467
}
6568

6669
renderMenus() {
70+
const { currentPath } = this.state
6771
return (
6872
<div className={styles.meuns}>
6973
<img
@@ -79,7 +83,7 @@ export default class Header extends React.Component {
7983
key={item.name}
8084
href={item.link}
8185
className={classnames({
82-
[styles.active]: location.pathname === item.active,
86+
[styles.active]: currentPath === item.active,
8387
})}
8488
>
8589
{item.name}
@@ -111,7 +115,7 @@ export default class Header extends React.Component {
111115

112116
render() {
113117
const { isBlankBg, hasBg, hasLightBg } = this.props
114-
const { showMenus } = this.state
118+
const { showMenus, currentPath } = this.state
115119
const isDarkBg = !isBlankBg && (this.state.isDarkBg || hasBg)
116120
const logoSrc = isBlankBg
117121
? '/images/op-logo.svg'
@@ -140,7 +144,7 @@ export default class Header extends React.Component {
140144
key={item.name}
141145
href={item.link}
142146
className={classnames({
143-
[styles.active]: location.pathname === item.active,
147+
[styles.active]: currentPath === item.active,
144148
})}
145149
>
146150
{item.name}

0 commit comments

Comments
 (0)