Skip to content

Commit db9e074

Browse files
committed
Fix use_search_params
1 parent 8b057b2 commit db9e074

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

docs/examples/python/use-query.py renamed to docs/examples/python/use-search-params.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from reactpy import component, html
1+
from reactpy import component, html, run
2+
23
from reactpy_router import browser_router, link, route, use_search_params
34

45

56
@component
67
def search():
78
query = use_search_params()
8-
return html.h1(f"Search Results for {query['q'][0]} 🔍")
9+
return html._(html.h1(f"Search Results for {query['q'][0]} 🔍"), html.p("Nothing (yet)."))
910

1011

1112
@component
@@ -18,5 +19,8 @@ def root():
1819
link("Search", to="/search?q=reactpy"),
1920
),
2021
),
21-
route("/about", html.h1("About Page 📖")),
22+
route("/search", search()),
2223
)
24+
25+
26+
run(root)

docs/src/learn/hooks.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ Several pre-fabricated hooks are provided to help integrate with routing feature
66

77
---
88

9-
## Use Query
9+
## Use Search Params
1010

1111
The [`use_search_params`][src.reactpy_router.use_search_params] hook can be used to access query parameters from the current location. It returns a dictionary of query parameters, where each value is a list of strings.
1212

1313
=== "components.py"
1414

1515
```python
16-
{% include "../../examples/python/use-query.py" %}
16+
{% include "../../examples/python/use-search-params.py" %}
1717
```
1818

1919
## Use Parameters

src/reactpy_router/core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ def link(*children: VdomChild, to: str, **attributes: Any) -> VdomDict:
9393

9494
def on_click(_event: dict[str, Any]) -> None:
9595
pathname, search = to.split("?", 1) if "?" in to else (to, "")
96+
if search:
97+
search = f"?{search}"
9698
set_location(Location(pathname, search))
9799

98100
attrs = {

tests/test_core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from reactpy import Ref, component, html, use_location
44
from reactpy.testing import DisplayFixture
5+
56
from reactpy_router import browser_router, link, route, use_params, use_search_params
67

78

@@ -136,7 +137,7 @@ def sample():
136137
await display.page.wait_for_selector("#success")
137138

138139

139-
async def test_use_query(display: DisplayFixture):
140+
async def test_search_params(display: DisplayFixture):
140141
expected_query: dict[str, Any] = {}
141142

142143
@component

0 commit comments

Comments
 (0)