@@ -3,14 +3,14 @@ import { Link } from 'react-router-dom';
3
3
4
4
let hashFragment = '' ;
5
5
let observer = null ;
6
- let asyncTimer = null ;
6
+ let asyncTimerId = null ;
7
7
8
8
function reset ( ) {
9
9
hashFragment = '' ;
10
10
if ( observer !== null ) observer . disconnect ( ) ;
11
- if ( asyncTimer !== null ) {
12
- window . clearTimeout ( asyncTimer ) ;
13
- asyncTimer = null ;
11
+ if ( asyncTimerId !== null ) {
12
+ window . clearTimeout ( asyncTimerId ) ;
13
+ asyncTimerId = null ;
14
14
}
15
15
}
16
16
@@ -24,19 +24,16 @@ function getElAndScroll() {
24
24
return false ;
25
25
}
26
26
27
- function setupMutationObserver ( ) {
28
- observer = new MutationObserver ( getElAndScroll ) ;
29
- }
30
-
31
27
function hashLinkScroll ( ) {
32
28
// Push onto callback queue so it runs after the DOM is updated
33
- setTimeout ( ( ) => {
29
+ window . setTimeout ( ( ) => {
34
30
if ( getElAndScroll ( ) === false ) {
35
- if ( observer === null ) setupMutationObserver ( ) ;
31
+ if ( observer === null ) {
32
+ observer = new MutationObserver ( getElAndScroll ) ;
33
+ }
36
34
observer . observe ( document , { attributes : true , childList : true , subtree : true } ) ;
37
- if ( asyncTimer !== null ) window . clearTimeout ( asyncTimer ) ;
38
35
// if the element doesn't show up in 10 seconds, stop checking
39
- asyncTimer = window . setTimeout ( ( ) => {
36
+ asyncTimerId = window . setTimeout ( ( ) => {
40
37
reset ( ) ;
41
38
} , 10000 ) ;
42
39
}
@@ -46,16 +43,13 @@ function hashLinkScroll() {
46
43
export function HashLink ( props ) {
47
44
function handleClick ( e ) {
48
45
if ( props . onClick ) props . onClick ( e ) ;
49
- let hash = '' ;
46
+ reset ( ) ;
50
47
if ( typeof props . to === 'string' ) {
51
- hash = props . to . split ( '#' ) . slice ( 1 ) . join ( '#' ) ;
48
+ hashFragment = props . to . split ( '#' ) . slice ( 1 ) . join ( '#' ) ;
52
49
} else if ( typeof props . to === 'object' && typeof props . to . hash === 'string' ) {
53
- hash = props . to . hash . replace ( '#' , '' ) ;
54
- }
55
- if ( hash !== '' ) {
56
- hashFragment = hash ;
57
- hashLinkScroll ( ) ;
50
+ hashFragment = props . to . hash . replace ( '#' , '' ) ;
58
51
}
52
+ if ( hashFragment !== '' ) hashLinkScroll ( ) ;
59
53
}
60
54
return < Link { ...props } onClick = { handleClick } > { props . children } </ Link > ;
61
55
}
0 commit comments