Browse Source

feat: add a redirect

Jonathan Kelley 3 năm trước cách đây
mục cha
commit
f47d2900fb

+ 0 - 1
examples/fermi.rs

@@ -1,7 +1,6 @@
 #![allow(non_snake_case)]
 
 use dioxus::prelude::*;
-use fermi::prelude::*;
 
 fn main() {
     dioxus::desktop::launch(app)

+ 1 - 0
packages/router/examples/simple.rs

@@ -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
packages/router/src/components/redirect.rs

@@ -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
 }