infer.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. use dioxus_core::{events::on::MouseEvent, prelude::*};
  2. use dioxus_web::WebsysRenderer;
  3. fn main() {
  4. // Setup logging
  5. wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
  6. console_error_panic_hook::set_once();
  7. log::info!("hello world");
  8. wasm_bindgen_futures::spawn_local(WebsysRenderer::start(Example));
  9. }
  10. // this is a component
  11. static Example: FC<()> = |ctx| {
  12. let (event, set_event) = use_state(&ctx, || None);
  13. let handler = move |evt: MouseEvent| {
  14. set_event(Some(evt));
  15. };
  16. log::info!("hello world");
  17. ctx.render(rsx! {
  18. div {
  19. class: "py-12 px-4 w-full max-w-2xl mx-auto bg-red-100"
  20. span {
  21. class: "text-sm font-semibold"
  22. "Dioxus Example: Synthetic Events"
  23. }
  24. button {
  25. class: "inline-block py-4 px-8 mr-6 leading-none text-white bg-indigo-600 hover:bg-indigo-900 font-semibold rounded shadow"
  26. "press me"
  27. }
  28. pre {
  29. onmousemove: {handler}
  30. id: "json"
  31. "Hello world"
  32. }
  33. Example2 { name: "Blah".into() }
  34. }
  35. })
  36. };
  37. #[derive(Debug, PartialEq, Props)]
  38. struct ExampleProps {
  39. name: String
  40. }
  41. static Example2: FC<ExampleProps> = |ctx| {
  42. ctx.render(rsx!{
  43. div {
  44. h1 {"hello {ctx.name}"}
  45. }
  46. })
  47. };