rustroutescomponentsdioxus

How can I pass a channel through Dioxus router for cross-component communication?


I'm trying to pass a channel from App to components through the Dioxus router so they can communicate between each other.

#[tokio::main]
async fn main() {
    dioxus_desktop::launch(App);
}

fn App(cx: Scope) -> Element {
    render! {
        style { include_str!("./style.css") },
        Router::<Route> {},
    }
}

#[derive(Clone, Routable, Debug, PartialEq)]
pub enum Route {
    #[route("/")]
    Login {},

    #[route("/chat")]
    Chat {
        name: String,
    },
}

I tried to use some kind of global and static state manager but the more I developed it the more complex it became and never got to conclude.


Solution

  • If you want a channel to communicate between components, you can use the use_coroutine hook. If you want shared state, you can use either Fermi or use_shared_state.

    If you want to try something experimental, you could even try the new dioxus signals crate on the git version of the framework. It can be used with the context API like use_shared_state, but should be more performant and it is always Copy regardless of the inner type