@@ -12,13 +12,13 @@ export function bind(node) {
12
12
} ;
13
13
}
14
14
15
- export function History ( { onHistoryChange } ) {
15
+ export function History ( { onHistoryChangeCallback } ) {
16
16
// Capture browser "history go back" action and tell the server about it
17
17
// Note: Browsers do not allow us to detect "history go forward" actions.
18
18
React . useEffect ( ( ) => {
19
19
// Register a listener for the "popstate" event and send data back to the server using the `onHistoryChange` callback.
20
20
const listener = ( ) => {
21
- onHistoryChange ( {
21
+ onHistoryChangeCallback ( {
22
22
pathname : window . location . pathname ,
23
23
search : window . location . search ,
24
24
} ) ;
@@ -34,29 +34,29 @@ export function History({ onHistoryChange }) {
34
34
// Tell the server about the URL during the initial page load
35
35
// FIXME: This currently runs every time any component is mounted due to a ReactPy core rendering bug.
36
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
- } , [ ] ) ;
37
+ // React.useEffect(() => {
38
+ // onHistoryChange({
39
+ // pathname: window.location.pathname,
40
+ // search: window.location.search,
41
+ // });
42
+ // return () => {};
43
+ // }, []);
44
44
return null ;
45
45
}
46
46
47
47
// FIXME: The Link component is unused due to a ReactPy core rendering bug
48
48
// which causes duplicate rendering (and thus duplicate event listeners).
49
49
// https://github.com/reactive-python/reactpy/pull/1224
50
- export function Link ( { onClick , linkClass } ) {
50
+ export function Link ( { onClickCallback , linkClass } ) {
51
51
// This component is not the actual anchor link.
52
52
// It is an event listener for the link component created by ReactPy.
53
53
React . useEffect ( ( ) => {
54
54
// Event function that will tell the server about clicks
55
55
const handleClick = ( event ) => {
56
56
event . preventDefault ( ) ;
57
57
let to = event . target . getAttribute ( "href" ) ;
58
- window . history . pushState ( { } , to , new URL ( to , window . location ) ) ;
59
- onClick ( {
58
+ window . history . pushState ( null , "" , new URL ( to , window . location ) ) ;
59
+ onClickCallback ( {
60
60
pathname : window . location . pathname ,
61
61
search : window . location . search ,
62
62
} ) ;
@@ -78,3 +78,20 @@ export function Link({ onClick, linkClass }) {
78
78
} ) ;
79
79
return null ;
80
80
}
81
+
82
+ export function Navigate ( { onNavigateCallback, to, replace } ) {
83
+ React . useEffect ( ( ) => {
84
+ if ( replace ) {
85
+ window . history . replaceState ( null , "" , new URL ( to , window . location ) ) ;
86
+ } else {
87
+ window . history . pushState ( null , "" , new URL ( to , window . location ) ) ;
88
+ }
89
+ onNavigateCallback ( {
90
+ pathname : window . location . pathname ,
91
+ search : window . location . search ,
92
+ } ) ;
93
+ return ( ) => { } ;
94
+ } , [ ] ) ;
95
+
96
+ return null ;
97
+ }
0 commit comments