update router book
This commit is contained in:
23 files changed
+212
-212
No files matched your search
@@ -17,11 +17,11 @@ Let's create a new `NavBar` component:
|
||||
# use dioxus_router::prelude::*;
|
||||
#
|
||||
fn NavBar(cx: Scope) -> Element {
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
nav {
|
||||
ul { }
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -40,7 +40,7 @@ more on other targets later) and an element. Let's add our links
|
||||
# use dioxus_router::prelude::*;
|
||||
#
|
||||
fn NavBar(cx: Scope) -> Element {
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
nav {
|
||||
ul {
|
||||
// new stuff starts here
|
||||
@@ -55,7 +55,7 @@ fn NavBar(cx: Scope) -> Element {
|
||||
// new stuff ends here
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -81,10 +81,10 @@ fn App(cx: Scope) -> Element {
|
||||
&|| Segment::content(comp(Home)).fallback(comp(PageNotFound))
|
||||
);
|
||||
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
NavBar { } // this is new
|
||||
Outlet { }
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
Now you should see a list of links near the top of your page. Click on one and
|
||||
@@ -103,7 +103,7 @@ in that case.
|
||||
# use dioxus_router::prelude::*;
|
||||
#
|
||||
fn NavBar(cx: Scope) -> Element {
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
nav {
|
||||
ul {
|
||||
li { Link {
|
||||
@@ -118,7 +118,7 @@ fn NavBar(cx: Scope) -> Element {
|
||||
} }
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -149,10 +149,10 @@ us to add a heading that tells the user they are on the blog.
|
||||
# use dioxus_router::prelude::*;
|
||||
#
|
||||
fn Blog(cx: Scope) -> Element {
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
h1 { "Blog" }
|
||||
Outlet {}
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -170,7 +170,7 @@ is selected:
|
||||
# use dioxus_router::prelude::*;
|
||||
#
|
||||
fn BlogList(cx: Scope) -> Element {
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
h2 { "Choose a post" }
|
||||
ul {
|
||||
li { Link {
|
||||
@@ -182,7 +182,7 @@ fn BlogList(cx: Scope) -> Element {
|
||||
"Read the second blog post"
|
||||
} }
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -199,16 +199,16 @@ parameters:
|
||||
struct PostId;
|
||||
|
||||
fn BlogPost(cx: Scope) -> Element {
|
||||
let route = use_route(&cx).unwrap();
|
||||
let route = use_route(cx).unwrap();
|
||||
|
||||
let post_id = route.parameter::<PostId>();
|
||||
let post = post_id
|
||||
.map(|id| id.to_string())
|
||||
.unwrap_or(String::from("unknown"));
|
||||
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
h2 { "Blog Post: {post}"}
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -243,10 +243,10 @@ fn App(cx: Scope) -> Element {
|
||||
}
|
||||
);
|
||||
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
NavBar { }
|
||||
Outlet { }
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -22,38 +22,38 @@ fn App(cx: Scope) -> Element {
|
||||
}
|
||||
);
|
||||
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
NavBar {}
|
||||
Outlet {}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn NavBar(cx: Scope) -> Element {
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
nav {
|
||||
ul {
|
||||
li { Link { target: named::<RootIndex>(), "Home" } }
|
||||
li { Link { target: "/blog", "Blog" } }
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn Home(cx: Scope) -> Element {
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
h1 { "Welcome to the Dioxus Blog!" }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn Blog(cx: Scope) -> Element {
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
h1 { "Blog" }
|
||||
Outlet {}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn BlogList(cx: Scope) -> Element {
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
h2 { "Choose a post" }
|
||||
ul {
|
||||
li { Link {
|
||||
@@ -65,27 +65,27 @@ fn BlogList(cx: Scope) -> Element {
|
||||
"Read the second blog post"
|
||||
} }
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
struct PostId;
|
||||
struct BlogPostName;
|
||||
fn BlogPost(cx: Scope) -> Element {
|
||||
let route = use_route(&cx).unwrap();
|
||||
let route = use_route(cx).unwrap();
|
||||
|
||||
let post_id = route.parameter::<PostId>();
|
||||
let post = post_id
|
||||
.map(|id| id.to_string())
|
||||
.unwrap_or(String::from("unknown"));
|
||||
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
h2 { "Blog Post: {post}"}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn PageNotFound(cx: Scope) -> Element {
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
h1 { "Page not found" }
|
||||
p { "We are terribly sorry, but the page you requested doesn't exist." }
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -14,9 +14,9 @@ First we need an actual page to route to! Let's add a homepage component:
|
||||
# use dioxus::prelude::*;
|
||||
#
|
||||
fn Home(cx: Scope) -> Element {
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
h1 { "Welcome to the Dioxus Blog!" }
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -49,9 +49,9 @@ fn App(cx: Scope) -> Element {
|
||||
&|| Segment::content(comp(Home))
|
||||
);
|
||||
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
Outlet { }
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -74,10 +74,10 @@ First, we create a new `PageNotFound` component.
|
||||
# use dioxus::prelude::*;
|
||||
#
|
||||
fn PageNotFound(cx: Scope) -> Element {
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
h1 { "Page not found" }
|
||||
p { "We are terribly sorry, but the page you requested doesn't exist." }
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -101,9 +101,9 @@ fn App(cx: Scope) -> Element {
|
||||
}
|
||||
);
|
||||
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
Outlet { }
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ If we need a link to an external page we can do it like this:
|
||||
# use dioxus_router::prelude::*;
|
||||
#
|
||||
fn GoToDioxus(cx: Scope) -> Element {
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
Link {
|
||||
target: NavigationTarget::External("https://dioxuslabs.com".into()),
|
||||
"Explicit ExternalTarget target"
|
||||
@@ -34,7 +34,7 @@ fn GoToDioxus(cx: Scope) -> Element {
|
||||
target: "https://dioxuslabs.com", // short form
|
||||
"Implicit ExternalTarget target"
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -105,7 +105,7 @@ Now we can change the targets of the links in our `BlogList` component.
|
||||
# fn BlogPost(cx: Scope) -> Element { unimplemented!() }
|
||||
#
|
||||
fn BlogList(cx: Scope) -> Element {
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
h2 { "Choose a post" }
|
||||
ul {
|
||||
li { Link {
|
||||
@@ -119,7 +119,7 @@ fn BlogList(cx: Scope) -> Element {
|
||||
"Read the second blog post"
|
||||
} }
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -142,14 +142,14 @@ We can change the link in our `NavBar` component to take advantage of that.
|
||||
# use dioxus_router::prelude::*;
|
||||
#
|
||||
fn NavBar(cx: Scope) -> Element {
|
||||
cx.render(rsx! {
|
||||
render! {
|
||||
nav {
|
||||
ul {
|
||||
li { Link { target: named::<RootIndex>(), "Home" } }
|
||||
li { Link { target: "/blog", "Blog" } }
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in new issue
Block a user