Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On the 0.6.0-alpha.4 version, clicking Route:: Link to route does not refresh locally, but reloads the whole page #3235

Open
pojoin opened this issue Nov 18, 2024 · 3 comments · May be fixed by #3630
Assignees
Labels
bug Something isn't working router Related to the router implementation
Milestone

Comments

@pojoin
Copy link

pojoin commented Nov 18, 2024

On the 0.6.0-alpha.4 version, clicking Route:: Link to route does not refresh locally, but reloads the whole page

@DogeDark
Copy link
Member

Would you be able to test if this occurs on 0.6.0-alpha.5? I have not noticed this issue. Additionally, would you be able to share the RSX code where you're using the Link?

@DogeDark DogeDark added bug Something isn't working router Related to the router implementation labels Nov 19, 2024
@pojoin
Copy link
Author

pojoin commented Nov 19, 2024

I am currently using version 0.6.0-alpha. 5
I use the code from the dioxus/example/route.rs
Insert println!("Rendering navbar"); into NavBar
When clicking on Link, the rendering will reload every time, but it works very well in version 0.4.3

Run it using the following command:

dx serve

Cargo.toml:

[package]
authors = ["pojoin"]
edition = "2021"
name = "test-1"
version = "0.0.1"


[dependencies]
dioxus = { version = "0.6.0-alpha.5", features=["desktop", "router"]}

main.rs:

//! An advanced usage of the router with nested routes and redirects.
//!
//! Dioxus implements an enum-based router, which allows you to define your routes in a type-safe way.
//! However, since we need to bake quite a bit of logic into the enum, we have to add some extra syntax.
//!
//! Note that you don't need to use advanced features like nest, redirect, etc, since these can all be implemented
//! manually, but they are provided as a convenience.

use dioxus::prelude::*;

const STYLE: Asset = asset!("/assets/router.css");

fn main() {
    dioxus::launch(|| {
        rsx! {
            document::Link { rel: "stylesheet", href: STYLE }
            Router::<Route> {}
        }
    });
}

// Turn off rustfmt since we're doing layouts and routes in the same enum
#[derive(Routable, Clone, Debug, PartialEq)]
#[rustfmt::skip]
enum Route {
    // Wrap Home in a Navbar Layout
    #[layout(NavBar)]
        // The default route is always "/" unless otherwise specified
        #[route("/")]
        Home {},

        // Wrap the next routes in a layout and a nest
        #[nest("/blog")]
        #[layout(Blog)]
            // At "/blog", we want to show a list of blog posts
            #[route("/")]
            BlogList {},

            // At "/blog/:name", we want to show a specific blog post, using the name slug
            #[route("/:name")]
            BlogPost { name: String },

        // We need to end the blog layout and nest
        // Note we don't need either - we could've just done `/blog/` and `/blog/:name` without nesting,
        // but it's a bit cleaner this way
        #[end_layout]
        #[end_nest]

    // And the regular page layout
    #[end_layout]

    // Add some redirects for the `/myblog` route
    #[nest("/myblog")]
        #[redirect("/", || Route::BlogList {})]
        #[redirect("/:name", |name: String| Route::BlogPost { name })]
    #[end_nest]

    // Finally, we need to handle the 404 page
    #[route("/:..route")]
    PageNotFound {
        route: Vec<String>,
    },
}

#[component]
fn NavBar() -> Element {
    println!("Rendering navbar");
    rsx! {
        nav { id: "navbar",
            Link { to: Route::Home {}, "Home" }
            Link { to: Route::BlogList {}, "Blog" }
        }
        Outlet::<Route> {}
    }
}

#[component]
fn Home() -> Element {
    rsx! { h1 { "Welcome to the Dioxus Blog!" } }
}

#[component]
fn Blog() -> Element {
    rsx! {
        h1 { "Blog" }
        Outlet::<Route> {}
    }
}

#[component]
fn BlogList() -> Element {
    rsx! {
        h2 { "Choose a post" }
        div { id: "blog-list",
            Link { to: Route::BlogPost { name: "Blog post 1".into() },
                "Read the first blog post"
            }
            Link { to: Route::BlogPost { name: "Blog post 2".into() },
                "Read the second blog post"
            }
        }
    }
}

// We can use the `name` slug to show a specific blog post
// In theory we could read from the filesystem or a database here
#[component]
fn BlogPost(name: String) -> Element {
    let contents = match name.as_str() {
        "Blog post 1" => "This is the first blog post. It's not very interesting.",
        "Blog post 2" => "This is the second blog post. It's not very interesting either.",
        _ => "This blog post doesn't exist.",
    };

    rsx! {
        h2 { "{name}" }
        p { "{contents}" }
    }
}

#[component]
fn PageNotFound(route: Vec<String>) -> Element {
    rsx! {
        h1 { "Page not found" }
        p { "We are terribly sorry, but the page you requested doesn't exist." }
        pre { color: "red", "log:\nattemped to navigate to: {route:?}" }
    }
}

bug

@ealmloff
Copy link
Member

Rerendering that component isn't optimal, but it shouldn't cause behavior issues. The more problematic issue is the navbar is recreated every time the route changes. Creating the navbar every time will reset any state in the component. The log still triggers if you put it in a use hook:

#[component]
fn NavBar() -> Element {
    use_hook(|| println!("creating navbar"));
    rsx! {
        nav { id: "navbar",
            Link { to: Route::Home {}, "Home" }
            Link { to: Route::BlogList {}, "Blog" }
        }
        Outlet::<Route> {}
    }
}

Both of the logs go away when you run the code in release mode. It might be caused by the signal generated in the debug mode expansion of the rsx! macro

@jkelleyrtp jkelleyrtp added this to the 0.6.2 milestone Jan 9, 2025
@jkelleyrtp jkelleyrtp modified the milestones: 0.6.2, 0.6.3 Jan 21, 2025
@ealmloff ealmloff self-assigned this Jan 22, 2025
@ealmloff ealmloff linked a pull request Jan 22, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working router Related to the router implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants