Skip to content

"No route matched" warning in yew-router #1661

@MakotoE

Description

@MakotoE

Question

I am getting this warning in my web browser on an app using yew-router.

src/router.rs:228 No route matched, provide a redirect prop to the router to handle cases where no route can be matched

I edited the counter example to add the router, passing both render and redirect props.

diff --git a/examples/counter/src/main.rs b/examples/counter/src/lib.rs
rename from examples/counter/src/main.rs
rename to examples/counter/src/lib.rs
--- a/examples/counter/src/main.rs	(revision d5ac6cdb85fc5f5f5a7a1fcdf6bf519832037bde)
+++ b/examples/counter/src/lib.rs	(date 1607923711863)
@@ -1,6 +1,10 @@
+#![recursion_limit="256"]
 use js_sys::Date;
 use yew::services::ConsoleService;
 use yew::{html, Component, ComponentLink, Html, ShouldRender};
+use yew::prelude::*;
+use yew_router::prelude::*;
+use wasm_bindgen::prelude::*;
 
 pub enum Msg {
     Increment,
@@ -66,6 +70,52 @@
     }
 }
 
-fn main() {
-    yew::start_app::<Model>();
+#[derive(Clone, Switch)]
+pub enum Route {
+    #[to = "/fileviewer"]
+    FileViewer,
+    #[to = "/"]
+    Index,
+}
+
+pub struct Page {}
+
+impl Component for Page {
+    type Message = ();
+    type Properties = ();
+
+    fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
+        Self{}
+    }
+
+    fn update(&mut self, msg: Self::Message) -> bool {
+        false
+    }
+
+    fn change(&mut self, _props: Self::Properties) -> bool {
+        false
+    }
+
+    fn view(&self) -> Html {
+        let render = Router::render(|switch: Route| match switch {
+            Route::FileViewer => html!{<p>{"fileviewer"}</p>},
+            Route::Index => html! {<Model />},
+        });
+        let redirect = Router::redirect(|route: yew_router::route::Route| {
+            ConsoleService::log("page not found");
+            Route::Index
+        });
+        html! {
+            <Router<Route>
+                render = render,
+                redirect = redirect,
+            />
+        }
+    }
+}
+
+#[wasm_bindgen(start)]
+pub fn start() {
+    wasm_logger::init(wasm_logger::Config::default());
+    yew::App::<Page>::new().mount_to_body();
 }

How do I change this code to fix the warning?

What I've tried (optional)

I looked at the router example but it's quite a complex program and I don't know what it does differently than mine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions