rsxt.rs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #![allow(non_snake_case)]
  2. use dioxus_core as dioxus;
  3. use dioxus::prelude::*;
  4. use dioxus_web::WebsysRenderer;
  5. fn main() {
  6. wasm_logger::init(wasm_logger::Config::new(log::Level::Trace));
  7. console_error_panic_hook::set_once();
  8. wasm_bindgen_futures::spawn_local(async {WebsysRenderer::new_with_props(Example, ExampleProps { initial_name: "..?"}).run().await.unwrap()} );
  9. }
  10. #[derive(PartialEq, Props)]
  11. struct ExampleProps {
  12. initial_name: &'static str
  13. }
  14. static Example: FC<ExampleProps> = |ctx, props| {
  15. let (name, set_name) = use_state(&ctx, move || props.initial_name);
  16. ctx.render(rsx! {
  17. div {
  18. class: "py-12 px-4 text-center w-full max-w-2xl mx-auto"
  19. span {
  20. class: "text-sm font-semibold"
  21. "Dioxus Example: Jack and Jill"
  22. }
  23. h2 {
  24. class: "text-5xl mt-2 mb-6 leading-tight font-semibold font-heading"
  25. "Hello, {name}"
  26. }
  27. button {
  28. class: "inline-block py-4 px-8 mr-6 leading-none text-white bg-indigo-600 hover:bg-indigo-900 font-semibold rounded shadow"
  29. onmouseover: move |_| set_name("jack")
  30. "Jack!"
  31. }
  32. button {
  33. class: "inline-block py-4 px-8 mr-6 leading-none text-white bg-indigo-600 hover:bg-indigo-900 font-semibold rounded shadow"
  34. onmouseover: move |_| set_name("jill")
  35. "Jill!"
  36. }
  37. button {
  38. class: "inline-block py-4 px-8 mr-6 leading-none text-white bg-indigo-600 hover:bg-indigo-900 font-semibold rounded shadow"
  39. onmouseover: move |_| set_name(props.initial_name)
  40. "Reset!"
  41. }
  42. Child {
  43. initial_name: "bill"
  44. }
  45. Child {
  46. initial_name: "bob"
  47. }
  48. }
  49. })
  50. };
  51. static Child: FC<ExampleProps> = |ctx, props| {
  52. ctx.render(rsx!{
  53. div {
  54. "Child name: {props.initial_name}"
  55. }
  56. })
  57. };