From 88e2bcfb9e4cf45fd6524fdf9d1bddec4b08e8e8 Mon Sep 17 00:00:00 2001 From: matt Date: Sun, 28 Nov 2021 05:15:48 -0700 Subject: [PATCH 1/8] Use From of AttrValue in Link component --- packages/yew-router/src/components/link.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/yew-router/src/components/link.rs b/packages/yew-router/src/components/link.rs index aef60c82bed..1053e8ed7f9 100644 --- a/packages/yew-router/src/components/link.rs +++ b/packages/yew-router/src/components/link.rs @@ -92,10 +92,7 @@ where e.prevent_default(); Msg::OnClick }); - let href: AttrValue = match BrowserHistory::route_to_url(to) { - Cow::Owned(href) => href.into(), - Cow::Borrowed(href) => href.into(), - }; + let href: AttrValue = BrowserHistory::route_to_url(to).into(); html! { Date: Sun, 28 Nov 2021 05:38:05 -0700 Subject: [PATCH 2/8] docs about `BrowserHistory::default()` --- website/docs/concepts/router.md | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/website/docs/concepts/router.md b/website/docs/concepts/router.md index 924d91903c5..f774909df3b 100644 --- a/website/docs/concepts/router.md +++ b/website/docs/concepts/router.md @@ -232,14 +232,11 @@ pub fn my_component() -> Html { } ``` -:::tip +:::caution The example here uses `Callback::once`. Use a normal callback if the target route can be the same with the route -the component is in. For example when you have a logo button on every page the that goes back to home when clicked, -clicking that button twice on home page causes the code to panic because the second click pushes an identical Home route -and won't trigger a re-render of the element. - -In other words, only use `Callback::once` when you are sure the target route is different. Or use normal callbacks only -to be safe. +the component is in, or just to play safe. For example, when you have a logo button on every page the that goes back to +home when clicked, clicking that button twice on home page causes the code to panic because the second click pushes an +identical Home route and the `use_history` hook won't trigger a re-render. ::: If you want to replace the current history instead of pushing a new history onto the stack, use `history.replace()` @@ -289,6 +286,12 @@ pub fn nav_items() -> Html { } ``` +:::tip +If your component only needs to set the route without listening to the changes, instead of the `use_history` +hook, `BrowserHistory::default()` can be used to acquire the global history instance. The latter also works in agent +environments. +::: + ##### Struct Components For struct components, the `AnyHistory` instance can be obtained through the `ctx.link().history()` API. The rest is From 05204e1ff7049e07939f3a6925f37ffc4a6dcf9a Mon Sep 17 00:00:00 2001 From: matt Date: Sun, 28 Nov 2021 06:08:06 -0700 Subject: [PATCH 3/8] remove unused cow import --- packages/yew-router/src/components/link.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/yew-router/src/components/link.rs b/packages/yew-router/src/components/link.rs index 1053e8ed7f9..c1932f0edcb 100644 --- a/packages/yew-router/src/components/link.rs +++ b/packages/yew-router/src/components/link.rs @@ -1,4 +1,3 @@ -use std::borrow::Cow; use std::marker::PhantomData; use serde::Serialize; @@ -92,7 +91,7 @@ where e.prevent_default(); Msg::OnClick }); - let href: AttrValue = BrowserHistory::route_to_url(to).into(); + let href = BrowserHistory::route_to_url(to); html! { Date: Sun, 28 Nov 2021 06:08:06 -0700 Subject: [PATCH 4/8] remove unused cow import --- packages/yew-router/src/components/link.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/yew-router/src/components/link.rs b/packages/yew-router/src/components/link.rs index 1053e8ed7f9..dac11579981 100644 --- a/packages/yew-router/src/components/link.rs +++ b/packages/yew-router/src/components/link.rs @@ -1,4 +1,3 @@ -use std::borrow::Cow; use std::marker::PhantomData; use serde::Serialize; @@ -92,7 +91,7 @@ where e.prevent_default(); Msg::OnClick }); - let href: AttrValue = BrowserHistory::route_to_url(to).into(); + let href = BrowserHistory::route_to_url(to).into(); html! { Date: Sun, 28 Nov 2021 06:16:02 -0700 Subject: [PATCH 5/8] lint --- packages/yew-router/src/components/link.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/yew-router/src/components/link.rs b/packages/yew-router/src/components/link.rs index c1932f0edcb..66a2421ca75 100644 --- a/packages/yew-router/src/components/link.rs +++ b/packages/yew-router/src/components/link.rs @@ -91,7 +91,7 @@ where e.prevent_default(); Msg::OnClick }); - let href = BrowserHistory::route_to_url(to); + let href: AttrValue = BrowserHistory::route_to_url(to).into(); html! { Date: Mon, 29 Nov 2021 03:17:40 -0700 Subject: [PATCH 6/8] More accurate `BrowserHistory::default()` tips --- website/docs/concepts/router.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/concepts/router.md b/website/docs/concepts/router.md index f774909df3b..fb5f429a9cf 100644 --- a/website/docs/concepts/router.md +++ b/website/docs/concepts/router.md @@ -288,8 +288,8 @@ pub fn nav_items() -> Html { :::tip If your component only needs to set the route without listening to the changes, instead of the `use_history` -hook, `BrowserHistory::default()` can be used to acquire the global history instance. The latter also works in agent -environments. +hook, `BrowserHistory::default()` can be used to acquire the global history instance. The latter also works in non-threaded agent +environments (`Context` and `Job`). This is a hack and a more idiomatic hook version will come later. ::: ##### Struct Components From 14bcddd86c81aec4cbe2b4a2a20b8342558b1e0a Mon Sep 17 00:00:00 2001 From: Matt <44753941+Madoshakalaka@users.noreply.github.com> Date: Mon, 29 Nov 2021 21:19:53 +0800 Subject: [PATCH 7/8] Update website/docs/concepts/router.md Co-authored-by: Julius Lungys <32368314+voidpumpkin@users.noreply.github.com> --- website/docs/concepts/router.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/website/docs/concepts/router.md b/website/docs/concepts/router.md index fb5f429a9cf..fc6196ff0e3 100644 --- a/website/docs/concepts/router.md +++ b/website/docs/concepts/router.md @@ -287,9 +287,10 @@ pub fn nav_items() -> Html { ``` :::tip -If your component only needs to set the route without listening to the changes, instead of the `use_history` +This is a hack and a more idiomatic hook version will come in the future! +But if your component only needs to set the route without listening to the changes, instead of the `use_history` hook, `BrowserHistory::default()` can be used to acquire the global history instance. The latter also works in non-threaded agent -environments (`Context` and `Job`). This is a hack and a more idiomatic hook version will come later. +environments (`Context` and `Job`). ::: ##### Struct Components From 525fe36df56494b86c3942090b659cf7cab44773 Mon Sep 17 00:00:00 2001 From: Matt <44753941+Madoshakalaka@users.noreply.github.com> Date: Sun, 5 Dec 2021 10:02:48 +0800 Subject: [PATCH 8/8] Update website/docs/concepts/router.md Co-authored-by: Simon --- website/docs/concepts/router.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/concepts/router.md b/website/docs/concepts/router.md index fc6196ff0e3..1809a6409be 100644 --- a/website/docs/concepts/router.md +++ b/website/docs/concepts/router.md @@ -234,7 +234,7 @@ pub fn my_component() -> Html { :::caution The example here uses `Callback::once`. Use a normal callback if the target route can be the same with the route -the component is in, or just to play safe. For example, when you have a logo button on every page the that goes back to +the component is in, or just to play safe. For example, when you have a logo button on every page, that goes back to home when clicked, clicking that button twice on home page causes the code to panic because the second click pushes an identical Home route and the `use_history` hook won't trigger a re-render. :::