fix: login example to use proper methods

This commit is contained in:
Jonathan Kelley committed 2022-02-16 14:11:31 -05:00
1 parent c6bdb5db76
commit babe862739
2 files changed
+12 -13

No files matched your search

+11 -12
View File
@@ -3,20 +3,20 @@
use dioxus::events::*; use dioxus::events::*;
use dioxus::prelude::*; use dioxus::prelude::*;
use dioxus::router::{Link, Route, Router, RouterService}; use dioxus::router::{use_router, Link, Route, Router};
fn main() { fn main() {
dioxus::desktop::launch(APP); dioxus::desktop::launch(app);
} }
static APP: Component = |cx| { fn app(cx: Scope) -> Element {
cx.render(rsx! { cx.render(rsx! {
Router { Router {
Route { to: "/", home() } Route { to: "/", home() }
Route { to: "/login", login() } Route { to: "/login", login() }
} }
}) })
}; }
fn home(cx: Scope) -> Element { fn home(cx: Scope) -> Element {
cx.render(rsx! { cx.render(rsx! {
@@ -26,7 +26,7 @@ fn home(cx: Scope) -> Element {
} }
fn login(cx: Scope) -> Element { fn login(cx: Scope) -> Element {
let service = cx.consume_context::<RouterService>()?; let service = use_router(&cx);
let onsubmit = move |evt: FormEvent| { let onsubmit = move |evt: FormEvent| {
to_owned![service]; to_owned![service];
@@ -42,10 +42,10 @@ fn login(cx: Scope) -> Element {
match resp { match resp {
// Parse data from here, such as storing a response token // Parse data from here, such as storing a response token
Ok(data) => service.push_route("/"), Ok(_data) => service.push_route("/"),
//Handle any errors from the fetch here //Handle any errors from the fetch here
Err(err) => {} Err(_err) => {}
} }
}); });
}; };
@@ -55,13 +55,12 @@ fn login(cx: Scope) -> Element {
form { form {
onsubmit: onsubmit, onsubmit: onsubmit,
prevent_default: "onsubmit", // Prevent the default behavior of <form> to post prevent_default: "onsubmit", // Prevent the default behavior of <form> to post
input { r#type: "text" }
input { "type": "text" }
label { "Username" } label { "Username" }
br {} br {}
input { r#type: "password" } input { "type": "password" }
label { label { "Password" }
"Password"
}
br {} br {}
button { "Login" } button { "Login" }
} }
+1 -1
View File
@@ -106,7 +106,7 @@ impl Drop for UseRouteListener {
} }
/// This hook provides access to the `RouterService` for the app. /// This hook provides access to the `RouterService` for the app.
pub fn use_router(cx: &ScopeState) -> &RouterService { pub fn use_router(cx: &ScopeState) -> &Rc<RouterService> {
cx.use_hook(|_| { cx.use_hook(|_| {
cx.consume_context::<RouterService>() cx.consume_context::<RouterService>()
.expect("Cannot call use_route outside the scope of a Router component") .expect("Cannot call use_route outside the scope of a Router component")