rustyew

How to import a component in Yew?


So I have a main.rs file that looks like so:

fn main() {
    yew::Renderer::<App>::new().render();
}

But I need to import the App component into main.rs. From what I saw in a tutorial I should be able to do: use <name_of_app>::App;, but that does not work. It will tell me that <name_of_app> is not a part of the crate for me to use and Rust analyzer is not coming up with suggestions.

I tried this approach, but Rust analyzer is complaining that App is being defined multiple times:

mod App;
use App::App;

fn main() {
    yew::Renderer::<App>::new().render();
}

Solution

  • You'll need to define a module for whatever file your App is defined in. mod name_of_app; use name_of_app::App;

    src/main.rs

    mod module_for_app;
    use module_for_app::App;
    
    fn main() {
        yew::Renderer::<App>::new().render();
    }
    

    src/module_for_app.rs

    pub struct App {
       // ....
    }
    

    See the section from The Book on modules for more information