Skip to content

Commit 4621208

Browse files
committed
Fix route outside of a Switch.
1 parent 1ce50ad commit 4621208

File tree

4 files changed

+3
-67
lines changed

4 files changed

+3
-67
lines changed

packages/yew-router/src/hooks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ pub fn use_route<R>() -> Option<R>
2828
where
2929
R: Routable + 'static,
3030
{
31-
use_context::<Option<R>>().and_then(|m| m)
31+
use_location()?.route::<R>()
3232
}

packages/yew-router/src/scope_ext.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@ pub struct HistoryHandle {
1010
_inner: ContextHandle<RouterState>,
1111
}
1212

13-
/// A [`ContextHandle`] for [`add_route_listener`](RouterScopeExt::add_route_listener).
14-
pub struct RouteHandle<R>
15-
where
16-
R: Routable + 'static,
17-
{
18-
_inner: ContextHandle<Option<R>>,
19-
}
20-
2113
/// An extension to [`Scope`](yew::html::Scope) that provides session history information.
2214
///
2315
/// You can access on `ctx.link()`
@@ -92,17 +84,6 @@ pub trait RouterScopeExt {
9284
/// when the handle is dropped. You need to keep the handle for as long as you need the
9385
/// callback.
9486
fn add_history_listener(&self, cb: Callback<AnyHistory>) -> Option<HistoryHandle>;
95-
96-
/// Adds a listener that gets notified when route changes.
97-
///
98-
/// # Note
99-
///
100-
/// [`RouteHandle`] works like a normal [`ContextHandle`] and it unregisters the callback
101-
/// when the handle is dropped. You need to keep the handle for as long as you need the
102-
/// callback.
103-
fn add_route_listener<R>(&self, cb: Callback<Option<R>>) -> Option<RouteHandle<R>>
104-
where
105-
R: Routable + 'static;
10687
}
10788

10889
impl<COMP: Component> RouterScopeExt for yew::html::Scope<COMP> {
@@ -119,20 +100,11 @@ impl<COMP: Component> RouterScopeExt for yew::html::Scope<COMP> {
119100
where
120101
R: Routable + 'static,
121102
{
122-
self.context::<Option<R>>(Callback::from(|_| {}))
123-
.and_then(|(m, _)| m)
103+
self.location()?.route()
124104
}
125105

126106
fn add_history_listener(&self, cb: Callback<AnyHistory>) -> Option<HistoryHandle> {
127107
self.context::<RouterState>(Callback::from(move |m: RouterState| cb.emit(m.history())))
128108
.map(|(_, m)| HistoryHandle { _inner: m })
129109
}
130-
131-
fn add_route_listener<R>(&self, cb: Callback<Option<R>>) -> Option<RouteHandle<R>>
132-
where
133-
R: Routable + 'static,
134-
{
135-
self.context::<Option<R>>(Callback::from(move |m: Option<R>| cb.emit(m)))
136-
.map(|(_, m)| RouteHandle { _inner: m })
137-
}
138110
}

packages/yew-router/src/switch.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,7 @@ where
102102
}
103103
};
104104

105-
html! {
106-
<ContextProvider<Option<R>> context={route}>
107-
{children}
108-
</ContextProvider<Option<R>>>
109-
}
105+
html! {<>{children}</>}
110106
}
111107
}
112108

packages/yew-router/src/utils.rs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ pub fn fetch_base_url() -> Option<String> {
4343

4444
#[cfg(test)]
4545
mod tests {
46-
// use serde::Serialize;
47-
// use std::collections::HashMap;
4846
use wasm_bindgen_test::wasm_bindgen_test as test;
4947
use yew::utils::*;
5048
use yew_router::prelude::*;
@@ -80,34 +78,4 @@ mod tests {
8078
.set_inner_html(r#"<base href="/base">"#);
8179
assert_eq!(fetch_base_url(), Some("/base".to_string()));
8280
}
83-
84-
// #[derive(Serialize, Clone)]
85-
// struct QueryParams {
86-
// foo: String,
87-
// bar: u32,
88-
// }
89-
90-
// #[test]
91-
// fn test_get_query_params() {
92-
// assert_eq!(
93-
// parse_query::<HashMap<String, String>>().unwrap(),
94-
// HashMap::new()
95-
// );
96-
97-
// let query = QueryParams {
98-
// foo: "test".to_string(),
99-
// bar: 69,
100-
// };
101-
102-
// yew_router::push_route_with_query(Routes::Home, query).unwrap();
103-
104-
// let params: HashMap<String, String> = parse_query().unwrap();
105-
106-
// assert_eq!(params, {
107-
// let mut map = HashMap::new();
108-
// map.insert("foo".to_string(), "test".to_string());
109-
// map.insert("bar".to_string(), "69".to_string());
110-
// map
111-
// });
112-
// }
11381
}

0 commit comments

Comments
 (0)