Fix basic routers
This commit is contained in:
1 parent
918d09b516
commit
b58eb04278
6 files changed
+22
-31
No files matched your search
@@ -10,7 +10,6 @@ use std::fmt::Display;
|
|||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
use dioxus_router::prelude::*;
|
use dioxus_router::prelude::*;
|
||||||
|
|
||||||
// ANCHOR: route
|
|
||||||
#[derive(Routable, Clone)]
|
#[derive(Routable, Clone)]
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
enum Route {
|
enum Route {
|
||||||
@@ -65,22 +64,22 @@ impl FromQuery for ManualBlogQuerySegments {
|
|||||||
#[component]
|
#[component]
|
||||||
fn BlogPost(query_params: ManualBlogQuerySegments) -> Element {
|
fn BlogPost(query_params: ManualBlogQuerySegments) -> Element {
|
||||||
rsx! {
|
rsx! {
|
||||||
div{"This is your blogpost with a query segment:"}
|
div { "This is your blogpost with a query segment:" }
|
||||||
div{ "{query_params:?}" }
|
div { "{query_params:?}" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
fn AutomaticBlogPost(name: String, surname: String) -> Element {
|
fn AutomaticBlogPost(name: String, surname: String) -> Element {
|
||||||
rsx! {
|
rsx! {
|
||||||
div{"This is your blogpost with a query segment:"}
|
div { "This is your blogpost with a query segment:" }
|
||||||
div{ "name={name}&surname={surname}" }
|
div { "name={name}&surname={surname}" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
fn App() -> Element {
|
fn App() -> Element {
|
||||||
rsx! { Router::<Route>{} }
|
rsx! { Router::<Route> {} }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ fn main() {
|
|||||||
launch_desktop(Route::Home {});
|
launch_desktop(Route::Home {});
|
||||||
}
|
}
|
||||||
|
|
||||||
// ANCHOR: router
|
|
||||||
#[derive(Routable, Clone)]
|
#[derive(Routable, Clone)]
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
enum Route {
|
enum Route {
|
||||||
@@ -30,7 +29,6 @@ enum Route {
|
|||||||
route: Vec<String>,
|
route: Vec<String>,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
// ANCHOR_END: router
|
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
fn NavBar() -> Element {
|
fn NavBar() -> Element {
|
||||||
|
|||||||
@@ -27,10 +27,7 @@ pub fn try_consume_context<T: 'static + Clone>() -> Option<T> {
|
|||||||
pub fn consume_context<T: 'static + Clone>() -> T {
|
pub fn consume_context<T: 'static + Clone>() -> T {
|
||||||
with_current_scope(|cx| cx.consume_context::<T>())
|
with_current_scope(|cx| cx.consume_context::<T>())
|
||||||
.flatten()
|
.flatten()
|
||||||
.expect(&format!(
|
.unwrap_or_else(|| panic!("Could not find context {}", std::any::type_name::<T>()))
|
||||||
"Could not find context {}",
|
|
||||||
std::any::type_name::<T>(),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Consume context from the current scope
|
/// Consume context from the current scope
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ dioxus-rsx = { workspace = true, optional = true }
|
|||||||
dioxus-signals = { workspace = true, optional = true }
|
dioxus-signals = { workspace = true, optional = true }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["macro", "html", "signals"]
|
default = ["macro", "html", "signals", "hooks"]
|
||||||
signals = ["dioxus-signals"]
|
signals = ["dioxus-signals"]
|
||||||
macro = ["dioxus-core-macro", "dioxus-rsx"]
|
macro = ["dioxus-core-macro", "dioxus-rsx"]
|
||||||
html = ["dioxus-html"]
|
html = ["dioxus-html"]
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ pub use dioxus_rsx as rsx;
|
|||||||
pub use dioxus_core_macro as core_macro;
|
pub use dioxus_core_macro as core_macro;
|
||||||
|
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
|
|
||||||
#[cfg(feature = "launch")]
|
#[cfg(feature = "launch")]
|
||||||
pub use crate::launch::*;
|
pub use crate::launch::*;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use dioxus_lib::prelude::ScopeId;
|
use dioxus_lib::prelude::*;
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
@@ -11,26 +11,22 @@ use crate::prelude::*;
|
|||||||
/// - [`None`], when the current component isn't a descendant of a [`Link`] component.
|
/// - [`None`], when the current component isn't a descendant of a [`Link`] component.
|
||||||
/// - Otherwise [`Some`].
|
/// - Otherwise [`Some`].
|
||||||
pub(crate) fn use_router_internal() -> Option<RouterContext> {
|
pub(crate) fn use_router_internal() -> Option<RouterContext> {
|
||||||
todo!()
|
let router = use_hook(|| {
|
||||||
|
let router = consume_context::<RouterContext>();
|
||||||
|
|
||||||
// let inner = cx.use_hook(|| {
|
let id = current_scope_id().expect("use_router_internal called outside of a component");
|
||||||
// let router = cx.consume_context::<RouterContext>()?;
|
router.subscribe(id);
|
||||||
|
|
||||||
// let id = cx.scope_id();
|
Some(router)
|
||||||
// router.subscribe(id);
|
});
|
||||||
|
|
||||||
// Some(Subscription { router, id })
|
use_on_destroy(|| {
|
||||||
// });
|
let router = consume_context::<RouterContext>();
|
||||||
// cx.use_hook(|| inner.as_ref().map(|s| s.router.clone()))
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Subscription {
|
let id = current_scope_id().expect("use_router_internal called outside of a component");
|
||||||
router: RouterContext,
|
|
||||||
id: ScopeId,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for Subscription {
|
router.unsubscribe(id);
|
||||||
fn drop(&mut self) {
|
});
|
||||||
self.router.unsubscribe(self.id);
|
|
||||||
}
|
router
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user