main.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // This test is used by playwright configured in the root of the repo
  2. // Tests:
  3. // - Server functions
  4. // - SSR
  5. // - Hydration
  6. #![allow(non_snake_case)]
  7. use dioxus::{prelude::*, CapturedError};
  8. fn main() {
  9. LaunchBuilder::fullstack().launch(app);
  10. }
  11. fn app() -> Element {
  12. let mut count = use_signal(|| 12345);
  13. let mut text = use_signal(|| "...".to_string());
  14. rsx! {
  15. h1 { "hello axum! {count}" }
  16. Title { "hello axum! {count}" }
  17. button { class: "increment-button", onclick: move |_| count += 1, "Increment" }
  18. button {
  19. class: "server-button",
  20. onclick: move |_| async move {
  21. if let Ok(data) = get_server_data().await {
  22. println!("Client received: {}", data);
  23. text.set(data.clone());
  24. post_server_data(data).await.unwrap();
  25. }
  26. },
  27. "Run a server function!"
  28. }
  29. "Server said: {text}"
  30. div {
  31. id: "errors",
  32. Errors {}
  33. }
  34. }
  35. }
  36. #[server(PostServerData)]
  37. async fn post_server_data(data: String) -> Result<(), ServerFnError> {
  38. println!("Server received: {}", data);
  39. Ok(())
  40. }
  41. #[server(GetServerData)]
  42. async fn get_server_data() -> Result<String, ServerFnError> {
  43. Ok("Hello from the server!".to_string())
  44. }
  45. #[server]
  46. async fn server_error() -> Result<String, ServerFnError> {
  47. tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
  48. Err(ServerFnError::new("the server threw an error!"))
  49. }
  50. #[component]
  51. fn Errors() -> Element {
  52. rsx! {
  53. // This is a tricky case for suspense https://github.com/DioxusLabs/dioxus/issues/2570
  54. // Root suspense boundary is already resolved when the inner suspense boundary throws an error.
  55. // We need to throw the error from the inner suspense boundary on the server to the hydrated
  56. // suspense boundary on the client
  57. ErrorBoundary {
  58. handle_error: |_| rsx! {
  59. "Hmm, something went wrong."
  60. },
  61. SuspenseBoundary {
  62. fallback: |_: SuspenseContext| rsx! {
  63. div {
  64. "Loading..."
  65. }
  66. },
  67. ThrowsError {}
  68. }
  69. }
  70. }
  71. }
  72. #[component]
  73. pub fn ThrowsError() -> Element {
  74. use_server_future(server_error)?
  75. .unwrap()
  76. .map_err(|err| RenderError::Aborted(CapturedError::from_display(err)))?;
  77. rsx! {
  78. "success"
  79. }
  80. }