dioxus/packages/hot-reload
2023-01-12 10:52:24 -06:00
..
src allow custom namespaces 2023-01-12 10:52:24 -06:00
Cargo.toml remove the hot-reload-macro crate 2023-01-11 18:51:09 -06:00
README.md allow custom namespaces 2023-01-12 10:52:24 -06:00

dioxus-hot-reload: Hot Reloading Utilites for Dioxus

Crates.io MIT licensed Build Status Discord chat

Website | Guides | API Docs | Chat

Overview

Dioxus supports hot reloading for static parts of rsx macros. This enables changing the styling of your application without recompiling the rust code. This is useful for rapid iteration on the styling of your application.

Hot reloading could update the following change without recompiling:

rsx! {
    div {
        "Count: {count}",
    }
}

=>

rsx! {
    div {
        color: "red",
        font_size: "2em",
        "Count: {count}",
    }
}

But it could not update the following change:

rsx! {
    div {
        "Count: {count}",
    }
}

=>

rsx! {
    div {
        "Count: {count*2}",
        onclick: |_| println!("clicked"),
    }
}

Usage

For hot relaoding with the web renderer, see the dioxus-cli project.

For renderers that support hot reloading add this to your main function before you launch your app to start the hot reloading server:

fn main(){
    hot_reload_init!();
    // launch your application
}

The dev server watches on the src and examples folders in the crate directory by default. To watch on custom paths pass the paths into the hot relaod macro:

fn main(){
    hot_reload_init!("src", "examples", "assets");
    // launch your application
}

By default the hot reloading server will output some logs in the console, to disable these logs pass the disable logging flag into the macro:

fn main(){
    hot_reload_init!("src", "examples", "assets", disable logging);
    // launch your application
}

If you are using a namespace other than html, you can implement the HotReloadingContext trait to provide a mapping between the rust names of your elements/attributes and the resultsing strings.

You can then provide the Context to the macro to make hot reloading work with your custom namespace:

fn main(){
    hot_reload_init!(@MyNamespace /*more configeration*/);
    // launch your application
}

Implementing hot reloading for a custom renderer

To add hot reloading support to your custom renderer you can use the connect function. This will connect to the dev server you just need to provide a way to transfer Templates to the VirtualDom. Once you implement this your users can use the hot_reload_init function just like any other render.

async fn launch(app: Component) {
    let mut vdom = VirtualDom::new(app);
    // ...

    let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
    dioxus_hot_reload::connect(move |template| {
        let _ = tx.send(template);
    });

    loop {
        tokio::select! {
            Some(template) = rx.recv() => {
                // update the template in the virtual dom
                vdom.replace_template(template);
            }
            _ = vdom.wait_for_work() => {
                // ...
            }
        }
        let mutations = vdom.render_immediate();
        // apply the mutations to the dom
    }
}

Contributing

  • Report issues on our issue tracker.
  • Join the discord and ask questions!

License

This project is licensed under the MIT license.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Dioxus by you shall be licensed as MIT without any additional terms or conditions.