瀏覽代碼

Fix basic routers

Evan Almloff 1 年之前
父節點
當前提交
b58eb04278

+ 5 - 6
examples/query_segments_demo/src/main.rs

@@ -10,7 +10,6 @@ use std::fmt::Display;
 use dioxus::prelude::*;
 use dioxus_router::prelude::*;
 
-// ANCHOR: route
 #[derive(Routable, Clone)]
 #[rustfmt::skip]
 enum Route {
@@ -65,22 +64,22 @@ impl FromQuery for ManualBlogQuerySegments {
 #[component]
 fn BlogPost(query_params: ManualBlogQuerySegments) -> Element {
     rsx! {
-        div{"This is your blogpost with a query segment:"}
-        div{ "{query_params:?}" }
+        div { "This is your blogpost with a query segment:" }
+        div { "{query_params:?}" }
     }
 }
 
 #[component]
 fn AutomaticBlogPost(name: String, surname: String) -> Element {
     rsx! {
-        div{"This is your blogpost with a query segment:"}
-        div{ "name={name}&surname={surname}" }
+        div { "This is your blogpost with a query segment:" }
+        div { "name={name}&surname={surname}" }
     }
 }
 
 #[component]
 fn App() -> Element {
-    rsx! { Router::<Route>{} }
+    rsx! { Router::<Route> {} }
 }
 
 fn main() {

+ 0 - 2
examples/router.rs

@@ -5,7 +5,6 @@ fn main() {
     launch_desktop(Route::Home {});
 }
 
-// ANCHOR: router
 #[derive(Routable, Clone)]
 #[rustfmt::skip]
 enum Route {
@@ -30,7 +29,6 @@ enum Route {
         route: Vec<String>,
     },
 }
-// ANCHOR_END: router
 
 #[component]
 fn NavBar() -> Element {

+ 1 - 4
packages/core/src/global_context.rs

@@ -27,10 +27,7 @@ pub fn try_consume_context<T: 'static + Clone>() -> Option<T> {
 pub fn consume_context<T: 'static + Clone>() -> T {
     with_current_scope(|cx| cx.consume_context::<T>())
         .flatten()
-        .expect(&format!(
-            "Could not find context {}",
-            std::any::type_name::<T>(),
-        ))
+        .unwrap_or_else(|| panic!("Could not find context {}", std::any::type_name::<T>()))
 }
 
 /// Consume context from the current scope

+ 1 - 1
packages/dioxus-lib/Cargo.toml

@@ -20,7 +20,7 @@ dioxus-rsx = { workspace = true, optional = true }
 dioxus-signals = { workspace = true, optional = true }
 
 [features]
-default = ["macro", "html", "signals"]
+default = ["macro", "html", "signals", "hooks"]
 signals = ["dioxus-signals"]
 macro = ["dioxus-core-macro", "dioxus-rsx"]
 html = ["dioxus-html"]

+ 1 - 0
packages/dioxus/src/lib.rs

@@ -28,6 +28,7 @@ pub use dioxus_rsx as rsx;
 pub use dioxus_core_macro as core_macro;
 
 pub mod prelude {
+
     #[cfg(feature = "launch")]
     pub use crate::launch::*;
 

+ 14 - 18
packages/router/src/utils/use_router_internal.rs

@@ -1,4 +1,4 @@
-use dioxus_lib::prelude::ScopeId;
+use dioxus_lib::prelude::*;
 
 use crate::prelude::*;
 
@@ -11,26 +11,22 @@ use crate::prelude::*;
 /// - [`None`], when the current component isn't a descendant of a [`Link`] component.
 /// - Otherwise [`Some`].
 pub(crate) fn use_router_internal() -> Option<RouterContext> {
-    todo!()
+    let router = use_hook(|| {
+        let router = consume_context::<RouterContext>();
 
-    // let inner = cx.use_hook(|| {
-    //     let router = cx.consume_context::<RouterContext>()?;
+        let id = current_scope_id().expect("use_router_internal called outside of a component");
+        router.subscribe(id);
 
-    //     let id = cx.scope_id();
-    //     router.subscribe(id);
+        Some(router)
+    });
 
-    //     Some(Subscription { router, id })
-    // });
-    // cx.use_hook(|| inner.as_ref().map(|s| s.router.clone()))
-}
+    use_on_destroy(|| {
+        let router = consume_context::<RouterContext>();
 
-struct Subscription {
-    router: RouterContext,
-    id: ScopeId,
-}
+        let id = current_scope_id().expect("use_router_internal called outside of a component");
+
+        router.unsubscribe(id);
+    });
 
-impl Drop for Subscription {
-    fn drop(&mut self) {
-        self.router.unsubscribe(self.id);
-    }
+    router
 }