rsxt.rs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 {
  9. let props = ExampleProps { initial_name: "..?", blarg: vec!["abc".to_string(), "abc".to_string()]};
  10. WebsysRenderer::new_with_props(Example, props)
  11. .run()
  12. .await
  13. .unwrap()
  14. });
  15. }
  16. #[derive(PartialEq, Props)]
  17. struct ExampleProps {
  18. initial_name: &'static str,
  19. blarg: Vec<String>
  20. }
  21. static Example: FC<ExampleProps> = |ctx, props| {
  22. let (name, set_name) = use_state(&ctx, move || props.initial_name);
  23. let sub = props.blarg.last().unwrap();
  24. ctx.render(rsx! {
  25. div {
  26. class: "py-12 px-4 text-center w-full max-w-2xl mx-auto"
  27. span {
  28. class: "text-sm font-semibold"
  29. "Dioxus Example: Jack and Jill"
  30. }
  31. h2 {
  32. class: "text-5xl mt-2 mb-6 leading-tight font-semibold font-heading"
  33. "Hello, {name}"
  34. }
  35. // CustomButton { name: sub, set_name: Box::new(move || set_name("jack")) }
  36. CustomButton { name: "Jack!", set_name: Box::new(move || set_name("jack")) }
  37. CustomButton { name: "Jill!", set_name: Box::new(move || set_name("jill")) }
  38. CustomButton { name: "Bill!", set_name: Box::new(move || set_name("Bill")) }
  39. CustomButton { name: "Reset!", set_name: Box::new(move || set_name(props.initial_name)) }
  40. }
  41. })
  42. };
  43. #[derive(Props)]
  44. struct ButtonProps<'src> {
  45. name: &'src str,
  46. // name: &'src str,
  47. set_name: Box< dyn Fn() + 'src>
  48. }
  49. impl PartialEq for ButtonProps<'_> {
  50. fn eq(&self, other: &Self) -> bool {
  51. false
  52. }
  53. }
  54. fn CustomButton<'a>(ctx: Context<'a>, props: &'a ButtonProps<'a>) -> DomTree {
  55. ctx.render(rsx!{
  56. button {
  57. class: "inline-block py-4 px-8 mr-6 leading-none text-white bg-indigo-600 hover:bg-indigo-900 font-semibold rounded shadow"
  58. onmouseover: move |_| (props.set_name)()
  59. "{props.name}"
  60. }
  61. })
  62. }