main.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //! Run with:
  2. //!
  3. //! ```sh
  4. //! dioxus build --features web
  5. //! cargo run --features ssr
  6. //! ```
  7. #![allow(non_snake_case, unused)]
  8. use dioxus::prelude::*;
  9. use dioxus_fullstack::prelude::*;
  10. use serde::{Deserialize, Serialize};
  11. fn main() {
  12. launch!(@([127, 0, 0, 1], 8080), app, (AppProps { count: 5 }), {
  13. incremental: IncrementalRendererConfig::default().invalidate_after(std::time::Duration::from_secs(120)),
  14. });
  15. }
  16. #[derive(Props, PartialEq, Debug, Default, Serialize, Deserialize, Clone)]
  17. struct AppProps {
  18. count: i32,
  19. }
  20. fn app(cx: Scope<AppProps>) -> Element {
  21. let mut count = use_state(cx, || cx.props.count);
  22. let text = use_state(cx, || "...".to_string());
  23. cx.render(rsx! {
  24. h1 { "High-Five counter: {count}" }
  25. button { onclick: move |_| count += 1, "Up high!" }
  26. button { onclick: move |_| count -= 1, "Down low!" }
  27. button {
  28. onclick: move |_| {
  29. to_owned![text];
  30. async move {
  31. if let Ok(data) = get_server_data().await {
  32. println!("Client received: {}", data);
  33. text.set(data.clone());
  34. post_server_data(data).await.unwrap();
  35. }
  36. }
  37. },
  38. "Run a server function"
  39. }
  40. "Server said: {text}"
  41. })
  42. }
  43. #[server(PostServerData)]
  44. async fn post_server_data(data: String) -> Result<(), ServerFnError> {
  45. // The server context contains information about the current request and allows you to modify the response.
  46. let cx = server_context();
  47. println!("Server received: {}", data);
  48. println!("Request parts are {:?}", cx.request_parts());
  49. Ok(())
  50. }
  51. #[server(GetServerData)]
  52. async fn get_server_data() -> Result<String, ServerFnError> {
  53. Ok("Hello from the server!".to_string())
  54. }