Skip to content

Commit 193cd2d

Browse files
committed
pushState and replaceState utils
1 parent e8904f9 commit 193cd2d

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/js/src/index.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "preact/compat";
22
import ReactDOM from "preact/compat";
3-
import { createLocationObject } from "./utils";
3+
import { createLocationObject, pushState, replaceState } from "./utils";
44
import {
55
HistoryProps,
66
LinkProps,
@@ -43,7 +43,6 @@ export function History({ onHistoryChangeCallback }: HistoryProps): null {
4343
// FIXME: This code is commented out since it currently runs every time any component
4444
// is mounted due to a ReactPy core rendering bug. `FirstLoad` component is used instead.
4545
// https://github.com/reactive-python/reactpy/pull/1224
46-
4746
// React.useEffect(() => {
4847
// onHistoryChange({
4948
// pathname: window.location.pathname,
@@ -69,7 +68,7 @@ export function Link({ onClickCallback, linkClass }: LinkProps): null {
6968
const handleClick = (event: MouseEvent) => {
7069
event.preventDefault();
7170
let to = (event.target as HTMLElement).getAttribute("href");
72-
window.history.pushState(null, "", new URL(to, window.location.href));
71+
pushState(to);
7372
onClickCallback(createLocationObject());
7473
};
7574

@@ -78,7 +77,7 @@ export function Link({ onClickCallback, linkClass }: LinkProps): null {
7877
if (link) {
7978
link.addEventListener("click", handleClick);
8079
} else {
81-
console.warn(`Link component with class name ${link} not found.`);
80+
console.warn(`Link component with class name ${linkClass} not found.`);
8281
}
8382

8483
// Delete the event listener when the component is unmounted
@@ -102,13 +101,13 @@ export function Navigate({
102101
}: NavigateProps): null {
103102
React.useEffect(() => {
104103
if (replace) {
105-
window.history.replaceState(null, "", new URL(to, window.location.href));
104+
replaceState(to);
106105
} else {
107-
window.history.pushState(null, "", new URL(to, window.location.href));
106+
pushState(to);
108107
}
109108
onNavigateCallback(createLocationObject());
110109
return () => {};
111-
}, [onNavigateCallback, to, replace]);
110+
}, []);
112111

113112
return null;
114113
}
@@ -123,7 +122,7 @@ export function FirstLoad({ onFirstLoadCallback }: FirstLoadProps): null {
123122
React.useEffect(() => {
124123
onFirstLoadCallback(createLocationObject());
125124
return () => {};
126-
}, [onFirstLoadCallback]);
125+
}, []);
127126

128127
return null;
129128
}

src/js/src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@ export function createLocationObject(): ReactPyLocation {
66
search: window.location.search,
77
};
88
}
9+
10+
export function pushState(to: string): void {
11+
window.history.pushState(null, "", new URL(to, window.location.href));
12+
}
13+
14+
export function replaceState(to: string): void {
15+
window.history.replaceState(null, "", new URL(to, window.location.href));
16+
}

0 commit comments

Comments
 (0)