Skip to content

Commit c86f37b

Browse files
committed
use_query -> use_search_params
1 parent 69598ef commit c86f37b

File tree

6 files changed

+20
-12
lines changed

6 files changed

+20
-12
lines changed

docs/examples/python/use-query.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from reactpy import component, html
2-
3-
from reactpy_router import link, route, simple, use_query
2+
from reactpy_router import link, route, simple, use_search_params
43

54

65
@component
76
def search():
8-
query = use_query()
7+
query = use_search_params()
98
return html.h1(f"Search Results for {query['q'][0]} 🔍")
109

1110

docs/src/learn/hooks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Several pre-fabricated hooks are provided to help integrate with routing feature
88

99
## Use Query
1010

11-
The [`use_query`][src.reactpy_router.use_query] 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.
11+
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

docs/src/learn/routers-routes-and-links.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Any route parameters collected from the current location then be accessed using
5454

5555
!!! warning "Pitfall"
5656

57-
While it is possible to use route parameters to capture values from query strings (such as `#!python /my/route/?foo={bar}`), this is not recommended. Instead, you should use the [`use_query`][src.reactpy_router.use_query] hook to access query string values.
57+
While it is possible to use route parameters to capture values from query strings (such as `#!python /my/route/?foo={bar}`), this is not recommended. Instead, you should use the [`use_search_params`][src.reactpy_router.use_search_params] hook to access query string values.
5858

5959
## Route Links
6060

src/reactpy_router/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
__version__ = "0.1.1"
33

44
from . import simple
5-
from .core import create_router, link, route, router_component, use_params, use_query
5+
from .core import create_router, link, route, router_component, use_params, use_search_params
66
from .types import Route, RouteCompiler, RouteResolver
77

88
__all__ = (
@@ -16,5 +16,5 @@
1616
"RouteResolver",
1717
"simple",
1818
"use_params",
19-
"use_query",
19+
"use_search_params",
2020
)

src/reactpy_router/core.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,22 @@ def use_params() -> dict[str, Any]:
8888
return _use_route_state().params
8989

9090

91-
def use_query(
91+
def use_search_params(
9292
keep_blank_values: bool = False,
9393
strict_parsing: bool = False,
9494
errors: str = "replace",
9595
max_num_fields: int | None = None,
9696
separator: str = "&",
9797
) -> dict[str, list[str]]:
98-
"""See :func:`urllib.parse.parse_qs` for parameter info."""
98+
"""
99+
The `use_search_params` hook is used to read and modify the query string in the URL \
100+
for the current location. Like React's own `use_state` hook, `use_search_params returns \
101+
an array of two values: the current location's search params and a function that may \
102+
be used to update them.
103+
104+
See `urllib.parse.parse_qs` for info on this hook's parameters."""
105+
106+
# FIXME: This needs to return a tuple of the search params and a function to update them
99107
return parse_qs(
100108
use_location().search[1:],
101109
keep_blank_values=keep_blank_values,

tests/test_core.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from reactpy import Ref, component, html, use_location
44
from reactpy.testing import DisplayFixture
5-
from reactpy_router import link, route, simple, use_params, use_query
5+
from reactpy_router import link, route, simple, use_params, use_search_params
66

77

88
async def test_simple_router(display: DisplayFixture):
@@ -40,7 +40,8 @@ def sample():
4040
root_element = await display.root_element()
4141
except AttributeError:
4242
root_element = await display.page.wait_for_selector(
43-
f"#display-{display._next_view_id}", state="attached" # type: ignore
43+
f"#display-{display._next_view_id}",
44+
state="attached", # type: ignore
4445
)
4546

4647
assert not await root_element.inner_html()
@@ -140,7 +141,7 @@ async def test_use_query(display: DisplayFixture):
140141

141142
@component
142143
def check_query():
143-
assert use_query() == expected_query
144+
assert use_search_params() == expected_query
144145
return html.h1({"id": "success"}, "success")
145146

146147
@component

0 commit comments

Comments
 (0)