#dioxus_router #[child] use?

8 messages · Page 1 of 1 (latest)

slate nymph
#

I'm updating a SPA from 0.3 to 0.4 and I'm stuck trying to put together a nested Route enum. Please excuse the brain farts as I'm jetlagged and taken two weeks off.

I see #[child] in Routable at https://docs.rs/dioxus-router/latest/dioxus_router/prelude/derive.Routable.html However this leads to compile errors (it expects parameters) so the example look wrong.

If I use #[nested("/asdf")] then I have to define all the sub-routes in the parent Routable as well as all the views (by views I mean the #[inline_props] fns). So how should I compose a SPA app?

I also think this would lead to a circular dependency since any part of the app should be able to build a Route for a link but that means a dependency on the top-level app itself. How is this resolved?

keen ermine
#

The example should be more like:

#![allow(non_snake_case)]

use dioxus::prelude::*;
use dioxus_router::prelude::*;
use std::str::FromStr;

fn main() {
    #[cfg(not(target_arch = "wasm32"))]
    dioxus_desktop::launch(root);

    #[cfg(target_arch = "wasm32")]
    dioxus_web::launch(root);
}

fn root(cx: Scope) -> Element {
    render! {
        Router::<Route> {}
    }
}

#[inline_props]
fn Route3(cx: Scope, dynamic: String) -> Element {
    todo!()
}

#[rustfmt::skip]
#[derive(Clone, Debug, PartialEq, Routable)]
enum Route {
    #[route("/:dynamic")]
    Route3 { dynamic: String },
    #[child("/testing")]
    Child {
        child: Route2
    }
}

#[rustfmt::skip]
#[derive(Clone, Debug, PartialEq, Routable)]
pub enum Route2 {
    #[route("/test")]
    Testing { },
}

fn Testing(cx: Scope) -> Element {
    todo!()
}
#

But I wouldn't use child yet, some things with that attributes are broken

#

If I use #[nested("/asdf")] then I have to define all the sub-routes in the parent Routable as well as all the views (by views I mean the #[inline_props] fns). So how should I compose a SPA app?
Yes, you will need to define all the routes in one central routable enum, but you should be able to pull out components into a seperate package

#

And once child is more complete, you will be able to seperate your router completely

#

I also think this would lead to a circular dependency since any part of the app should be able to build a Route for a link but that means a dependency on the top-level app itself. How is this resolved?

Circular dependencies could be an issue. What does your package structure look like?

If you need to you could create a types package that has the route enum, then in another package that provides implements the routable trait. You could use the types package without any circular dependancies

slate nymph
#

Thanks for the reply, I'll try it out tomorrow. The central enum is fine (and what I'm hoping for) but I didn't notice the Routable trait

#

For circular: it's I have things like /component1 and /component2. They're, for the most part, completely separate parts of an admin interface but any part of the site could feasibly link to any other. I think the Routable trait would separate it just fine though