feat: add a redirect
This commit is contained in:
1 parent
83be3df42a
commit
f47d2900fb
3 files changed
+13
-3
No files matched your search
@@ -1,7 +1,6 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use dioxus::prelude::*;
|
||||
use fermi::prelude::*;
|
||||
|
||||
fn main() {
|
||||
dioxus::desktop::launch(app)
|
||||
|
||||
@@ -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 {} }
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user