login_form.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //! This example demonstrates the following:
  2. //! Futures in a callback, Router, and Forms
  3. use dioxus::events::*;
  4. use dioxus::prelude::*;
  5. use dioxus::router::{use_router, Link, Route, Router};
  6. fn main() {
  7. dioxus::desktop::launch(app);
  8. }
  9. fn app(cx: Scope) -> Element {
  10. let onsubmit = move |evt: FormEvent| {
  11. cx.spawn(async move {
  12. let resp = reqwest::Client::new()
  13. .post("http://localhost/login")
  14. .form(&[
  15. ("username", &evt.values["username"]),
  16. ("password", &evt.values["password"]),
  17. ])
  18. .send()
  19. .await;
  20. match resp {
  21. // Parse data from here, such as storing a response token
  22. Ok(_data) => {
  23. println!("Login successful");
  24. }
  25. //Handle any errors from the fetch here
  26. Err(_err) => {}
  27. }
  28. });
  29. };
  30. cx.render(rsx! {
  31. h1 { "Login" }
  32. form {
  33. onsubmit: onsubmit,
  34. prevent_default: "onsubmit", // Prevent the default behavior of <form> to post
  35. input { "type": "text" }
  36. label { "Username" }
  37. br {}
  38. input { "type": "password" }
  39. label { "Password" }
  40. br {}
  41. button { "Login" }
  42. }
  43. })
  44. }