@@ -12,13 +12,13 @@ export function bind(node) {
1212 } ;
1313}
1414
15- export function History ( { onBrowserBack } ) {
15+ export function History ( { onHistoryChange } ) {
1616 // 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.
1818 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.
2020 const listener = ( ) => {
21- onBrowserBack ( {
21+ onHistoryChange ( {
2222 pathname : window . location . pathname ,
2323 search : window . location . search ,
2424 } ) ;
@@ -30,6 +30,17 @@ export function History({ onBrowserBack }) {
3030 // Delete the event listener when the component is unmounted
3131 return ( ) => window . removeEventListener ( "popstate" , listener ) ;
3232 } ) ;
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+ } , [ ] ) ;
3344 return null ;
3445}
3546
@@ -49,15 +60,17 @@ export function Link({ onClick, linkClass }) {
4960 } ;
5061
5162 // 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+ }
5567
5668 // Delete the event listener when the component is unmounted
5769 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+ }
6174 } ;
6275 } ) ;
6376 return null ;
0 commit comments