Skip to content

Commit 88d1f46

Browse files
committed
use server side prevent default
1 parent f57b63b commit 88d1f46

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/reactpy_router/components.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Any
55
from uuid import uuid4
66

7-
from reactpy import component, html
7+
from reactpy import component, event, html
88
from reactpy.backend.types import Location
99
from reactpy.core.types import VdomChild, VdomDict
1010
from reactpy.web.module import export, module_from_file
@@ -26,15 +26,16 @@ def link(*children: VdomChild, to: str, **attributes: Any) -> VdomDict:
2626
# properly sets the location. When a client-server communication layer is added to a \
2727
# future ReactPy release, this component will need to be rewritten to use that instead. \
2828
set_location = _use_route_state().set_location
29-
class_uuid = f"link-{uuid4().hex}"
3029

30+
@event(prevent_default=True)
3131
def on_click(_event: dict[str, Any]) -> None:
3232
pathname, search = to.split("?", 1) if "?" in to else (to, "")
3333
if search:
3434
search = f"?{search}"
3535
set_location(Location(pathname, search))
3636

37-
class_name = class_uuid
37+
uuid_string = f"link-{uuid4().hex}"
38+
class_name = f"{uuid_string}"
3839
if "className" in attributes:
3940
class_name = " ".join([attributes.pop("className"), class_name])
4041
# TODO: This can be removed when ReactPy stops supporting underscores in attribute names
@@ -45,9 +46,9 @@ def on_click(_event: dict[str, Any]) -> None:
4546
**attributes,
4647
"href": to,
4748
"onClick": on_click,
48-
"className": class_uuid,
49+
"className": class_name,
4950
}
50-
return html._(html.a(attrs, *children), html.script(link_js_content.replace("UUID", class_uuid)))
51+
return html._(html.a(attrs, *children), html.script(link_js_content.replace("UUID", uuid_string)))
5152

5253

5354
def route(path: str, element: Any | None, *routes: Route) -> Route:

src/reactpy_router/static/link.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
document.querySelector(".UUID").addEventListener(
22
"click",
33
(event) => {
4-
event.preventDefault();
54
let to = event.target.getAttribute("href");
65
window.history.pushState({}, to, new URL(to, window.location));
76
},

0 commit comments

Comments
 (0)