1
1
import React from "preact/compat" ;
2
2
import ReactDOM from "preact/compat" ;
3
- import { createLocationObject } from "./utils" ;
3
+ import { createLocationObject , pushState , replaceState } from "./utils" ;
4
4
import {
5
5
HistoryProps ,
6
6
LinkProps ,
@@ -43,7 +43,6 @@ export function History({ onHistoryChangeCallback }: HistoryProps): null {
43
43
// FIXME: This code is commented out since it currently runs every time any component
44
44
// is mounted due to a ReactPy core rendering bug. `FirstLoad` component is used instead.
45
45
// https://github.com/reactive-python/reactpy/pull/1224
46
-
47
46
// React.useEffect(() => {
48
47
// onHistoryChange({
49
48
// pathname: window.location.pathname,
@@ -69,7 +68,7 @@ export function Link({ onClickCallback, linkClass }: LinkProps): null {
69
68
const handleClick = ( event : MouseEvent ) => {
70
69
event . preventDefault ( ) ;
71
70
let to = ( event . target as HTMLElement ) . getAttribute ( "href" ) ;
72
- window . history . pushState ( null , "" , new URL ( to , window . location . href ) ) ;
71
+ pushState ( to ) ;
73
72
onClickCallback ( createLocationObject ( ) ) ;
74
73
} ;
75
74
@@ -78,7 +77,7 @@ export function Link({ onClickCallback, linkClass }: LinkProps): null {
78
77
if ( link ) {
79
78
link . addEventListener ( "click" , handleClick ) ;
80
79
} else {
81
- console . warn ( `Link component with class name ${ link } not found.` ) ;
80
+ console . warn ( `Link component with class name ${ linkClass } not found.` ) ;
82
81
}
83
82
84
83
// Delete the event listener when the component is unmounted
@@ -102,13 +101,13 @@ export function Navigate({
102
101
} : NavigateProps ) : null {
103
102
React . useEffect ( ( ) => {
104
103
if ( replace ) {
105
- window . history . replaceState ( null , "" , new URL ( to , window . location . href ) ) ;
104
+ replaceState ( to ) ;
106
105
} else {
107
- window . history . pushState ( null , "" , new URL ( to , window . location . href ) ) ;
106
+ pushState ( to ) ;
108
107
}
109
108
onNavigateCallback ( createLocationObject ( ) ) ;
110
109
return ( ) => { } ;
111
- } , [ onNavigateCallback , to , replace ] ) ;
110
+ } , [ ] ) ;
112
111
113
112
return null ;
114
113
}
@@ -123,7 +122,7 @@ export function FirstLoad({ onFirstLoadCallback }: FirstLoadProps): null {
123
122
React . useEffect ( ( ) => {
124
123
onFirstLoadCallback ( createLocationObject ( ) ) ;
125
124
return ( ) => { } ;
126
- } , [ onFirstLoadCallback ] ) ;
125
+ } , [ ] ) ;
127
126
128
127
return null ;
129
128
}
0 commit comments