feat: add a redirect

This commit is contained in:
Jonathan Kelley committed 2022-03-05 14:06:54 -05:00
1 parent 83be3df42a
commit f47d2900fb
3 files changed
+13 -3

No files matched your search

-1
View File
@@ -1,7 +1,6 @@
#![allow(non_snake_case)]
use dioxus::prelude::*;
use fermi::prelude::*;
fn main() {
dioxus::desktop::launch(app)
+1
View File
@@ -20,6 +20,7 @@ fn app(cx: Scope) -> Element {
Link { to: "/blog", li { "blog" }}
Link { to: "/blog/tim", li { "tims' blog" }}
Link { to: "/blog/bill", li { "bills' blog" }}
Link { to: "/apples", li { "go to apples" }}
}
Route { to: "/", Home {} }
Route { to: "/blog/", BlogList {} }
+12 -2
View File
@@ -37,8 +37,18 @@ pub struct RedirectProps<'a> {
pub fn Redirect<'a>(cx: Scope<'a, RedirectProps<'a>>) -> Element {
let router = use_router(&cx);
// todo: check if the current location matches the "from" pattern
router.replace_route(cx.props.to, None, None);
let immediate_redirect = cx.use_hook(|_| {
if let Some(from) = cx.props.from {
router.register_total_route(from.to_string(), cx.scope_id());
false
} else {
true
}
});
if *immediate_redirect || router.should_render(cx.scope_id()) {
router.replace_route(cx.props.to, None, None);
}
None
}