Skip to content

Commit 7041e85

Browse files
committed
Replace History initial effect - Is now mandatory for the ReactPy executor to provide the initial URL
1 parent 034e9d3 commit 7041e85

5 files changed

Lines changed: 17 additions & 24 deletions

File tree

src/js/bun.lockb

0 Bytes
Binary file not shown.

src/js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
"prettier": "^3.3.3"
1010
},
1111
"dependencies": {
12-
"@reactpy/client": "^1.0.3"
12+
"@reactpy/client": "^1.1.0"
1313
}
1414
}

src/js/src/components.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ export function bind(node: HTMLElement | Element | Node) {
2222
/**
2323
* History component that captures browser "history go back" actions and notifies the server.
2424
*/
25-
export function History({ onHistoryChangeCallback }: HistoryProps): null {
25+
export function History({ onHistoryPreviousCallback }: HistoryProps): null {
2626
// Tell the server about history "popstate" events
2727
React.useEffect(() => {
2828
const listener = () => {
29-
onHistoryChangeCallback(createLocationObject());
29+
onHistoryPreviousCallback(createLocationObject());
3030
};
3131

3232
// Register the event listener
@@ -35,12 +35,6 @@ export function History({ onHistoryChangeCallback }: HistoryProps): null {
3535
// Delete the event listener when the component is unmounted
3636
return () => window.removeEventListener("popstate", listener);
3737
});
38-
39-
// Tell the server about the URL during the initial page load
40-
React.useEffect(() => {
41-
onHistoryChangeCallback(createLocationObject());
42-
return () => {};
43-
}, []);
4438
return null;
4539
}
4640

src/js/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export interface ReactPyLocation {
44
}
55

66
export interface HistoryProps {
7-
onHistoryChangeCallback: (location: ReactPyLocation) => void;
7+
onHistoryPreviousCallback: (location: ReactPyLocation) => void;
88
}
99

1010
export interface LinkProps {

src/reactpy_router/routers.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,33 +59,32 @@ def router(
5959
User notice: This component typically should never be used. Instead, use `create_router` if creating
6060
a custom routing engine."""
6161

62-
old_connection = use_connection()
63-
location, set_location = use_state(cast("Location | None", None))
62+
initial = use_connection()
63+
location, set_location = use_state(initial.location)
6464
resolvers = use_memo(
6565
lambda: tuple(map(resolver, _iter_routes(routes))),
6666
dependencies=(resolver, hash(routes)),
6767
)
68-
route_element = None
69-
match = use_memo(lambda: _match_route(resolvers, location or old_connection.location))
68+
match = use_memo(lambda: _match_route(resolvers, location or initial.location))
7069

7170
if match:
72-
# Skip rendering until ReactPy-Router knows what URL the page is on.
73-
if location:
74-
route_element = _route_state_context(
75-
match.element,
76-
value=RouteState(set_location, match.params),
71+
if not location or not location.path:
72+
raise RuntimeError(
73+
"ReactPy-Router was unable to determine the current URL location.\n"
74+
"Are you sure you are running this within the a ConnectionContext?"
7775
)
7876

79-
def on_history_change(event: dict[str, Any]) -> None:
80-
"""Callback function used within the JavaScript `History` component."""
77+
def on_history_previous(event: dict[str, Any]) -> None:
78+
"""Callback function used within the JavaScript `History` component that signifies
79+
a history "go back" action."""
8180
new_location = Location(**event)
8281
if location != new_location:
8382
set_location(new_location)
8483

8584
return ConnectionContext(
86-
History({"onHistoryChangeCallback": on_history_change}), # type: ignore[return-value]
87-
route_element,
88-
value=Connection(old_connection.scope, location or old_connection.location, old_connection.carrier),
85+
History({"onHistoryPreviousCallback": on_history_previous}), # type: ignore[return-value]
86+
_route_state_context(match.element, value=RouteState(set_location, match.params)),
87+
value=Connection(initial.scope, location or initial.location, initial.carrier),
8988
)
9089

9190
return None

0 commit comments

Comments
 (0)