@@ -12,13 +12,13 @@ export function bind(node) {
12
12
} ;
13
13
}
14
14
15
- export function History ( { onBrowserBack } ) {
15
+ export function History ( { onHistoryChange } ) {
16
16
// Capture browser "history go back" action and tell the server about it
17
- // Note: Browsers do not allow you to detect "history go forward" actions.
17
+ // Note: Browsers do not allow us to detect "history go forward" actions.
18
18
React . useEffect ( ( ) => {
19
- // Register a listener for the "popstate" event and send data back to the server using the `onBrowserBack ` callback.
19
+ // Register a listener for the "popstate" event and send data back to the server using the `onHistoryChange ` callback.
20
20
const listener = ( ) => {
21
- onBrowserBack ( {
21
+ onHistoryChange ( {
22
22
pathname : window . location . pathname ,
23
23
search : window . location . search ,
24
24
} ) ;
@@ -30,6 +30,17 @@ export function History({ onBrowserBack }) {
30
30
// Delete the event listener when the component is unmounted
31
31
return ( ) => window . removeEventListener ( "popstate" , listener ) ;
32
32
} ) ;
33
+
34
+ // Tell the server about the URL during the initial page load
35
+ // FIXME: This currently runs every time any component is mounted due to a ReactPy core rendering bug.
36
+ // https://github.com/reactive-python/reactpy/pull/1224
37
+ React . useEffect ( ( ) => {
38
+ onHistoryChange ( {
39
+ pathname : window . location . pathname ,
40
+ search : window . location . search ,
41
+ } ) ;
42
+ return ( ) => { } ;
43
+ } , [ ] ) ;
33
44
return null ;
34
45
}
35
46
@@ -49,15 +60,17 @@ export function Link({ onClick, linkClass }) {
49
60
} ;
50
61
51
62
// Register the event listener
52
- document
53
- . querySelector ( `.${ linkClass } ` )
54
- . addEventListener ( "click" , handleClick ) ;
63
+ let link = document . querySelector ( `.${ linkClass } ` ) ;
64
+ if ( link ) {
65
+ link . addEventListener ( "click" , handleClick ) ;
66
+ }
55
67
56
68
// Delete the event listener when the component is unmounted
57
69
return ( ) => {
58
- document
59
- . querySelector ( `.${ linkClass } ` )
60
- . removeEventListener ( "click" , handleClick ) ;
70
+ let link = document . querySelector ( `.${ linkClass } ` ) ;
71
+ if ( link ) {
72
+ link . removeEventListener ( "click" , handleClick ) ;
73
+ }
61
74
} ;
62
75
} ) ;
63
76
return null ;
0 commit comments